angularjs - Angular - compare many old and new values -
using angular, possible compare old , new value when changed (programmatically, or otherwise)?
i guess i'm after functionality similar ng-change can applied element , triggered whenever value in expression updated.
of course, use $scope.$watch on entire data model, find individual changed values (and injecting 'changedirection' property) feels excessive.
edit: consider data model of array of 500 objects, each 5 properties, 2 of integers need know if have increased/decreased.
you can use $watch on dot delimited paths signify individual properties in model wish watch.
<!doctype html> <html> <head> <script data-require="angular.js@1.4.8" data-semver="1.4.8" src="https://code.angularjs.org/1.4.8/angular.js"></script> <link rel="stylesheet" href="style.css" /> <script src="script.js"></script> </head> <body> <div ng-controller="ctrl"> <input ng-model="model.name.first" />update:{{watchvals}} </div> <script> var app = angular.module("app", []); app.controller("ctrl", ["$scope", function($scope) { $scope.model = { "name": { "first": "john", "last": "doe" } }; $scope.val = ""; $scope.$watch("model.name.first", function(newval, oldval) { $scope.watchvals = oldval + " -> " + newval; }); } ]); angular.bootstrap(document, [app.name]); </script> </body> </html>
Comments
Post a Comment