angular / angular.js

AngularJS - HTML enhanced for web apps!
https://angularjs.org
MIT License
58.83k stars 27.51k forks source link

Proposal : Use a custom compare function for objectEquality in $scope.$watch #10069

Open Toilal opened 9 years ago

Toilal commented 9 years ago

It would be useful to be able to pass a function as the 3rd parameter objectEquality of $scope.$watch.

If a function is passed, is will be used instead of angular.equals to check for object equality.

var eq = function(newData, oldData) {
    ...
}

var cb = function() {
    ...
}

$scope.$watch('data',  cb, eq);

It could also provide better performance by implementing specific equality function for watched object.

My concrete use case : I have momentJS objects inside deep data to watch. But angular.equals doesn't work well with momentJS. For equality checking on momentJS object, moment#isSame must be used instead. Using this new functional parameter, i could delegate equality to lodash#isEqual which is able to customize object equality.

lgalfaso commented 9 years ago

This was discussed before #2871, the main issue always came back to one of these points

That said, I would like to have a PR in place and discuss these point there

Toilal commented 9 years ago

@lgalfaso This the PR here : https://github.com/angular/angular.js/pull/10096