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(); });
Comments
Post a Comment