angularjs移除不必要的$watch
2013-08-20 00:40 by 破狼, 383 阅读, 2 评论, 收藏, 编辑
在我们的web page,特别是移动设备上,太多的angular $watch将可能导致性能问题。这篇文章将解释如何去移除额外的$watch提高你的应用程序性能。
$watch如果不再使用,我们最好将其释放掉,在angular中我们可以自由的选择在什么时候将$watch从$watch列表中移除。
让我们来看个示例:
app = angular.module('app', []);
app.controller('MainCtrl', function($scope) {
$scope.updated = 0;
$scope.stop = function() {
textWatch();
};
var textWatch = $scope.$watch('text', function(newVal, oldVal) {
if (newVal === oldVal) { return; }
$scope.updated++;
});
});
<body ng-controller="MainCtrl">
<input type="text" ng-model="text" /> {{updated}} times updated.
<button ng-click="stop()">Stop count</button>
</body>
补充:web前端 , JavaScript ,