Search code examples
angularangular2-templateangular2-components

Change data binded?


Is there a way in Angular 2 to change the data binded to an element by ngModel ? For example :

<input [(ngModel)]="color" />

How stop binding color, and bind color2 (for example) and the input ? Thank you !


Solution

  • You can use a function to achieve this behavior. Something like:

    my.component.ts

    ...
    getModelValue(){
        if(contidionA){
            return colorA;
        } else {
            return colorB;
        }
    }
    ...
    

    my.component.html

    <input [(ngModel)]="getModelValue()" />