Is there a tidy way to define a large watch collection for AngularJS? -


i using code:

        $scope.$watchcollection('[config.examid, config.pagetype, config.createdby, config.modifiedby, config.reference]',             function (newvalue, oldvalue) {                 if (typeof newvalue !== 'undefined' && typeof oldvalue !== 'undefined' && newvalue !== oldvalue) {                     _u.putconfigs($scope.config);                     //$scope.grid.data = null;                 };              }); 

now have add more items collection. there way can neatly spread these on multiple lines? understand (might wrong). watchcollection has string.

well, looks use simple fields inside of configuration object can modify have multiple watch (which can more flexible in way). if have complex objects can watch them using deep flag:

var app = angular.module('app', []); app.controller('configctrl', function($scope, $parse, $log) {     $scope.config = {};      // initial configuration     $scope.watchproperties = ['examid', 'pagetype', 'createdby',                            'modifiedby'];      // dynamic modifications watched properties     $scope.configurewatchproperties = function() {         $scope.watchproperties.push('reference');     }      $scope.updateconfig = function() {         // todo: update config here         $log.log('config updated:', $scope.config);     }      $scope.addwatchproperty = function(context, property, deep) {         $scope.$watch(function() {             return $parse(property)(context);         }, function(newvalue, oldvalue) {             if (newvalue && newvalue !== oldvalue) {                 $scope.updateconfig();             }         }, deep);     }      $scope.configurewatch = function() {         angular.foreach($scope.watchproperties, function(prop) {             $scope.addwatchproperty($scope.config, prop);         });     }      $scope.configurewatchproperties();     $scope.configurewatch(); }); 

complete jsfiddle example:


Comments

Popular posts from this blog

c# - Sort XmlNodeList with a specific Node value -

Android Java.Lang.RuntimeException : Unable to start activity Component Info -

actionscript 3 - Equivalent to moveReceived in ElectroServer 5 -