I need to update a little digital 'clock' in my Angular app. I'm using the following code in my controller:
我需要在Angular应用程序中更新一个小数字'时钟'。我在我的控制器中使用以下代码:
$scope.time = new Date();
var interval = $timeout(function updateTime() {
console.log("Update time");
$scope.time = new Date();
$scope.formattedTimeValue = WBUtils.formattedTime($scope.time);
interval = $timeout(updateTime, 1000);
}, 1000);
I display formattedTimeValue
in a little div in my html using {{formattedTimeValue}}
expression. I also have an ng-repeat directive formatting data using filters. My problem is those filters get reevaluated every second. I can't understand why. I've changed my interval function to the following but filters still get reevaluated:
我使用{{formattedTimeValue}}表达式在我的html中的一个小div中显示formattedTimeValue。我还有一个ng-repeat指令使用过滤器格式化数据。我的问题是那些过滤器每秒都会被重新评估。我不明白为什么。我已将间隔功能更改为以下内容,但仍会重新评估过滤器:
var interval = $timeout(function updateTime() {
console.log("Update time");
interval = $timeout(updateTime, 1000);
}, 1000);
Can someone please explain to me why my filter gets evaluated each second for each object (unchanged) in ng-repeat. The filter currently looks like this:
有人可以向我解释为什么我的过滤器每秒对每个对象进行评估(不变)在ng-repeat中。过滤器目前看起来像这样:
module.filter('formatLogRecord', function () {
return function (log) {
console.log("Filtering");
return "";
}
});
2 个解决方案
#1
1
The angular $timeout service includes making a call to $apply
for the whole $rootScope ( https://github.com/angular/angular.js/blob/v1.2.0rc1/src/ng/timeout.js#L10 ), which will cause a new digest and re-evaluate the filters. If that's not what you want, just use a plain js setTimeout instead and wrap only the bits dealing with scope in $scope.$apply
yourself.
angular $ timeout服务包括调用$ apply for full $ rootScope(https://github.com/angular/angular.js/blob/v1.2.0rc1/src/ng/timeout.js#L10),这将导致新的摘要并重新评估过滤器。如果这不是您想要的,只需使用普通的js setTimeout,并在$ scope中仅包含处理范围的位。$自己应用。
#2
1
First, $timeout causes a $scope.$digest() when the timer reaches 0, which re-evaluates the bindings. Second, each time you iterate your time you are changing the value of $scope.time and $scope.formattedTimeValue, both of which will cause Angular to run a digest and re-evaluate your values.
首先,$ timeout在计时器达到0时导致$ scope。$ digest(),重新计算绑定。其次,每次迭代时间都会改变$ scope.time和$ scope.formattedTimeValue的值,这两个值都会导致Angular运行摘要并重新评估您的值。
#1
1
The angular $timeout service includes making a call to $apply
for the whole $rootScope ( https://github.com/angular/angular.js/blob/v1.2.0rc1/src/ng/timeout.js#L10 ), which will cause a new digest and re-evaluate the filters. If that's not what you want, just use a plain js setTimeout instead and wrap only the bits dealing with scope in $scope.$apply
yourself.
angular $ timeout服务包括调用$ apply for full $ rootScope(https://github.com/angular/angular.js/blob/v1.2.0rc1/src/ng/timeout.js#L10),这将导致新的摘要并重新评估过滤器。如果这不是您想要的,只需使用普通的js setTimeout,并在$ scope中仅包含处理范围的位。$自己应用。
#2
1
First, $timeout causes a $scope.$digest() when the timer reaches 0, which re-evaluates the bindings. Second, each time you iterate your time you are changing the value of $scope.time and $scope.formattedTimeValue, both of which will cause Angular to run a digest and re-evaluate your values.
首先,$ timeout在计时器达到0时导致$ scope。$ digest(),重新计算绑定。其次,每次迭代时间都会改变$ scope.time和$ scope.formattedTimeValue的值,这两个值都会导致Angular运行摘要并重新评估您的值。