gpt4 book ai didi

javascript - 为什么 AngularJS 过滤器只运行一次?

转载 作者:搜寻专家 更新时间:2023-11-01 05:03:43 25 4
gpt4 key购买 nike

考虑以下示例:

    angular.module('app', []).controller('TestController', function($scope) {
$scope.getText = function() {
console.log('getting text');
return 'text';
};
}).filter('text', function() {
return function() {
console.log('text filter');
return 'text';
};
});
 

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.2/angular.min.js"></script>
<div ng-app="app" ng-controller="TestController">
<p>{{getText()}}</p>
<p>{{'' | text}}</p>
</div>

请注意,getText() 函数运行两次,而过滤器仅运行一次。我假设 getText() 函数运行两次以确保模型现在稳定。为什么过滤器的行为不同?

最佳答案

关于这个主题的文档非常清楚:

In templates, filters are only executed when their inputs have changed. This is more performant than executing a filter on each $digest as is the case with expressions.

Here's the source.

关于javascript - 为什么 AngularJS 过滤器只运行一次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42722611/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com