我找不出如何將字段綁定到組件,以便在更改OnDataUpdate()中的屬性時字段會更新。當Angular2屬性發生更改時,數據綁定不會更新
字段「OtherValue」具有綁定到輸入字段的雙向工作方式,「Name」字段顯示組件時顯示「test」。但是,當我刷新數據,沒有任何字段更新顯示更新的數據。
「this.name」的第一個記錄值是未定義的(???),第二個是正確的,但綁定到相同屬性的字段不會更新。
組件如何爲名稱字段提供初始值,但是當觸發數據更新時,名稱屬性突然未定義?
stuff.component.ts
@Component({
moduleId: __moduleName,
selector: 'stuff',
templateUrl: 'stuff.component.html'
})
export class BuildingInfoComponent {
Name: string = "test";
OtherValue: string;
constructor(private dataservice: DataSeriesService) {
dataservice.subscribe(this.OnDataUpdate);
}
OnDataUpdate(data: any) {
console.log(this.Name);
this.Name = data.name;
this.OtherValue = data.otherValue;
console.log(this.Name);
}
stuff.component.html
<table>
<tr>
<th>Name</th>
<td>{{Name}}</td>
</tr>
<tr>
<th>Other</th>
<td>{{OtherValue}}</td>
</tr>
</table>
<input [(ngModel)]="OtherValue" />
如果它不是函數調用並且是值賦值? @pierreduc –
然後你應該把這個任務包裝在一個匿名的箭頭函數中 – PierreDuc