I can't figure out how to bind the fields to the component so that the fields update when i change the properties in OnDataUpdate().
The field "OtherValue" has a working two way binding to the input-field and the field for "Name" displayes "test" when the component is displayed. But when i refresh the data, none of the fields are updated to display the updated data.
The first logged value of "this.name" is undefined(???), the second is correct, but the field bound to the same property does not update.
How can the component provide the initial value for the name-field, but when the data update is trigged, the name-property is suddenly undefined?
stuff.component.ts
@Component({
moduleId: __moduleName,
selector: 'stuff',
templateUrl: 'stuff.component.html'
})
export class StuffComponent {
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" />
You have main issue is that you have used ngModel for select element. So when you select item from select element at that time value is changed in selectedIds[rowIndex] item. I have applied minor code refactoring in first div as per below it will helpful to you.
Angular v2+ supports two-way data binding using ngModel directive and also by having getter and setter methods.
Attribute binding syntax resembles property binding, but instead of an element property between brackets, you precede the name of the attribute with the prefix attr , followed by a dot. Then, you set the attribute value with an expression that resolves to a string.
As mentioned earlier, one-way data binding in Angular can be of three types i.e Interpolation, Property binding, and Event binding.
The this
context is lost if you pass it like that in the subscribe()
function. You can fix this in several ways:
by using bind
constructor(private dataservice: DataSeriesService) {
dataservice.subscribe(this.OnDataUpdate.bind(this));
}
by using an anonymous arrow function wrapper
constructor(private dataservice: DataSeriesService) {
dataservice.subscribe((data : any) => {
this.OnDataUpdate(data);
});
}
change the declaration of the function
OnDataUpdate = (data: any) : void => {
console.log(this.Name);
this.Name = data.name;
this.OtherValue = data.otherValue;
console.log(this.Name);
}
Passing method references this way breaks the this
reference
dataservice.subscribe(this.OnDataUpdate);
use this instead:
dataservice.subscribe((value) => this.OnDataUpdate(value));
By using ()=>
(arrow function) this
is retained and keeps referring to the current class instance.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With