AngularJS - Blur + Changed?

Viewed 22665

What is the easiest way to combine ng-changed and ng-blur?

I've found this post: How to let ng-model not update immediately?

However, this does no longer work in angluar 1.2+ Is there any way to achieve the same behavior?

I guess I have to store a copy of the old value myself and compare the new value to that on blur if I try to do the same, or is there any easier way ?

9 Answers

In app.html

<input type="text" name="name" ng-model="$ctrl.user.name" ng-blur="$ctrl.saveChanges()" ng-change="$ctrl.onFieldChange()"/>

in app.ts

 public onFieldChange() {
    this.isValuesModified = true;
}

//save changes on blur event
public saveChanges() {
    //check if value is modified
    if(this.isValuesModified){

     //Do your stuff here
    }
}

This is how I resolved this.

<input ng-focus="oldValue=value" ng-model="value" ng-blur="oldValue!=value && youFunction()" />
Related