【发布时间】:2016-03-04 00:30:13
【问题描述】:
这是我的设置(超级简化):
模板:
<button (click)="doNothing">Show</button>
<h2>{{count}}</h2>
组件:
count: number;
constructor(private _dataService: DataService) {
this.init();
}
private init(): void {
this._dataService.getAll().subscribe(res=> { //just a simple http GET here
this.count = res;
console.log(this.count); //logs the correct value, but view not updated
});
}
private doNothing(): void { }
您可能已经注意到,我使用的 button 没有做任何事情,但除非我单击它,否则我的视图不会更新为正确的值(我花了一些时间来考虑这个 hack)。我在这里实际上在做什么,我怎样才能用不那么愚蠢的东西代替它?我发现的所有 http 样本都向我保证,这应该可以在没有任何魔法的情况下工作,但显然它没有。我正在使用 beta6
【问题讨论】:
-
尝试将
this.init()调用从构造函数移动到ngOnInit() -
@Sasxa 我试过了,没有帮助
-
似乎应该可以工作:plnkr.co/edit/yFGC8fC1HOw6D9xp4hOS?p=preview .
-
我在这里有几个 http 调用填充列表:syntaxsuccess.com/angular-2-samples/#/demo/http 不确定这是您要查找的内容,但您可以在此处查看代码:github.com/thelgevold/angular-2-samples/blob/master/components/…
标签: angular