Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Data binding not updating when property changes in Angular2

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" />
like image 981
Kristoffer Berge Avatar asked May 25 '16 13:05

Kristoffer Berge


People also ask

Why binding is not working in Angular?

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.

Is angular2 support two-way data binding only?

Angular v2+ supports two-way data binding using ngModel directive and also by having getter and setter methods.

What is difference between property binding and attribute binding in Angular?

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.

What are the 3 types of data binding available with Angular?

As mentioned earlier, one-way data binding in Angular can be of three types i.e Interpolation, Property binding, and Event binding.


2 Answers

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);
}
like image 67
Poul Kruijt Avatar answered Sep 28 '22 19:09

Poul Kruijt


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.

like image 22
Günter Zöchbauer Avatar answered Sep 28 '22 19:09

Günter Zöchbauer