Using the following routing definition:
export const routes: Routes = [
{ path: '', redirectTo: '/home', pathMatch: 'full' },
{ path: 'home', component: GeneralComponent },
{ path: 'test', component: TestComponent }
];
export const routing: ModuleWithProviders = RouterModule.forRoot(routes);
I have the following for loop set up in a module that is loaded as a part of the "home" loading sequence:
<div *ngFor="let row of rows; let i = index">
<div class="rows">
<div *ngFor="let coach of row">
<bio [coach]='coach'></bio>
</div>
</div>
</div>
and this is backed by a simple component on the backend:
export class TeamListingComponent implements OnInit {
rows: Array<Array<Coach>>;
constructor(private service: CoachListingService) { }
ngOnInit () {
this.rows = this.service.get();
}
}
On initial page load to .../home, everything looks and works great.
If I navigate to .../test and then back to .../home though, the for loop doesn't seem to be fired off. The HTML-only content loads fine but the supplied content from the for loop doesn't render. (No errors reported in JS console).
I've confirmed "this.rows" gets reset properly and has content on the return trip and both the service appears to be working as expected and the non- "for loop" HTML in the component loads up and presents exactly as expected.
Versions:
*ng --version*
angular-cli: 1.0.0-beta.24
node: 7.0.0
os: darwin x64
@angular/common: 2.4.10
@angular/compiler: 2.4.10
@angular/core: 2.4.10
@angular/forms: 2.4.10
@angular/http: 2.4.10
@angular/platform-browser: 2.4.10
@angular/platform-browser-dynamic: 2.4.10
@angular/router: 3.4.10
@angular/compiler-cli: 2.4.10
UPDATE:
Following the comments listed below suggesting approaches with the NgZone module, I reviewed the posts & subsequent NgZone API page and I added the following to the component in question:
constructor(private service: CoachListingService, private zone: NgZone) {
this.zone.runOutsideAngular(() => {
this._updateList(() => {
console.log(this.rows);
this.zone.run(() => { console.log('Forcing re-draw', this.rows); });
});
});
}
ngOnInit () {
this.rows = this.service.get();
}
_updateList (doneCallback: () => void) {
this.rows = this.service.get();
}
This seemed to have no effect. I'm sure I'm not thinking this through correctly.
I later altered "_updateList" to call the callback with:
_updateList (doneCallback: () => void) {
this.rows = this.service.get();
doneCallback();
}
and it does indeed show the proper output in the javascript console. So data is present.
Thoughts?
TIA