The array that is used in the ngfor loop, when it is updated how do you get Angular2 to re-run that expression? Currently it wont output anything due it running at the beginnin with an empty array - once updated (the array has been changed) the ngfor expression is not re-run - which is the desired functionality. How is this done?
For example in the component HTML;
<option *ngFor="let user of users" [value]="user.id">{{user.name}}</option>
In the component TS we have declared a property;
users: IUser[] = [];
In ngOnInit we have a call to a TS service that returns us the user data;
this.userDataService.getUsers().subscribe(data => {
this.users = data;
});
Updated code example;
@Component({
selector: 'app-manage-user',
templateUrl: './user-form.component.html',
providers: [UsersDataService]
})
export class UserFormComponent implements OnInit {
constructor(private userDataService: UsersDataService) {
}
users: IUser[] = [];
ngOnInit() {
this.userDataService.getUsers().subscribe(user => {
this.users = user;
});
}
}
this.userDataService.getUsers....method?*ngForchecks the array for changes every time change detection is run and updates the DOM accordingly. Are you using exactly thengOnInitcode above or is it more complex in your real code?