OnDataUpdate()のプロパティを変更するとフィールドが更新されるように、フィールドをコンポーネントにバインドする方法がわかりません。プロパティがAngular2で変更されたときにデータバインディングが更新されない
"OtherValue"フィールドは、入力フィールドに有効な双方向バインディングを持ち、 "Name"フィールドは、コンポーネントが表示されたときに "test"を表示します。しかし、データを更新すると、更新されたデータを表示するためにフィールドが更新されません。
"this.name"の最初に記録された値は未定義(???)で、2番目は正しいものの、同じプロパティにバインドされたフィールドは更新されません。
コンポーネントはどのようにして名前フィールドの初期値を提供できますか?データ更新がトリガーされると、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この方法は、この代わりにthis
参照
dataservice.subscribe(this.OnDataUpdate);
使用を破る方法参照を渡す
<table>
<tr>
<th>Name</th>
<td>{{Name}}</td>
</tr>
<tr>
<th>Other</th>
<td>{{OtherValue}}</td>
</tr>
</table>
<input [(ngModel)]="OtherValue" />
これは、関数呼び出しではなく、値の代入ですか? @pierreduc –
次に、その割り当てを無名矢印関数で囲む必要があります – PierreDuc