This code snippet is all about managing an array in Angular. The Injectable decorator is used to define a service called Svc with methods for setting and getting column definitions.
import { Injectable } from '@angular/core';
@Injectable()
export class Svc {
constructor() {}
private _updatedArray: Array < any > = [];
setColDefs(columns: any) {
this._updatedArray.push(columns);
console.log(columns);
}
getColDef() {
return this._updatedArray;
}
}
An example of setting the array in one component:
...
this._listSvc.setColDefs(colDefArray);
...
And in another component:
...
import { Svc } from '../services/mySvc.svc';
@Component({
...
})
export class EditComponent implements OnInit, OnDestroy{
constructor(
private _editCompSvc: Svc
){}
ngOnInit() {
console.log(this._editCompSvc.getColDef());
}
The issue reported is that the getColDef function always returns an empty list even though columns were added. Any suggestions or explanations for this behavior would be greatly appreciated. Thank you!
Additional module information:
import { NgModule } from '@angular/core';
import { routing } from './routes/myroute.route';
import { Svc } from './services/mySvc.svc'
import { ListComponent } from './actions/list.comp';
import { EditComponent } from './actions/edit.comp';
@NgModule({
imports:
[
routing
],
declarations:
[
ListComponent,
EditComponent
],
providers:
[Svc]
})
export class Module { }