AngularJS trick

You’re using AngularJS with some form and don’t want to hassle with event listeners when changing a property of an object. Usually, the ng-change directive works only with the new value of whatever you toss at it, but you can send the old value as well.

Simply add curly brackets to the parameter and it’ll send a copy of the old untouched data.

The function declaration of setChangedStatus looks like this:

$scope.setChangedStatus = function(newValue, oldValue) {
    // newValue contains the entire object after the change
    // oldValue contains the entire object before the change
    // note: don't set any values in oldValue, as they will get lost

This might be helpful, if you need to compare a number of other properties in the car object and need to respond to that.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.