To update the data on the screen in AngularJS2, you must use Forms . You can read about it on this page or in more detail here . If I understand Forms correctly, you should change the yout @View part as follows:
@View({ template: ` <div *for="#user of users" control="users"> {{user}} </div> `, directives: [For] })
- edited
Try the following:
import {Component, View, bootstrap, For} from 'angular2/angular2'; // Annotation section @Component({ selector: 'app' }) @View({ template: ` <div [control]="users" *for="#user of users"> {{user.value}} </div> `, directives: [For, forms] }) class App { users:Array<String>; constructor() { this.users = new Control([]); this.fillUsersAsync(); } fillUsersAsync(){ window['fetch']('http://jsonplaceholder.typicode.com/users') .then( resp => resp.json()) .then( users => var usersArr = [] users.forEach(user => usersArr.push(user.name)) this.users = new Control(usersArr)); } } bootstrap(App);
I am not sure that my answer is absolutely right, but I can not find more information. Experiment with this code, and Strength may be with you! :)
Also I found this link , very informative.
As I understand it, when in the constructor you have to initialize the youre users variable using new Control(some data) , and then in the view template you can access this data as follows: {{users.value}} .
If this does not work: try to do the same, but with very simple data, for example. use a string instead of an array.
And this video will help not to give out forms in ng2.
James schermann
source share