[英]How data binding is working in filters in AngularJS?
This is my button 这是我的按钮
<button ng-repeat="item in data" ng-click="selectCategory(item)">{{item}</button>
And this is my controller 这是我的控制器
.controller("productListCtrl", function ($scope) {
let selectedCategory = null;
$scope.selectCategory = function (item) {
selectedCategory = item;
};
$scope.categoryFilterFn = function (product) {
return selectedCategory == null || product.category == selectedCategory;
}
});
I have created method categoryFilterFn
for filter
filter and I use it in other part of my code 我已经为
filter
器filter
器创建了categoryFilterFn
方法,并在代码的其他部分中使用了该方法
<div ng-repeat="item in data| filter:categoryFilterFn" class="well">
so I use filter
for this div. 所以我为这个div使用
filter
。 When the page is loaded first time filter is working, but when I click button it goes to selectCategory()
function and changes the value of selectedCategory
variable(which is not even a $scope
property, just a variable). 当页面第一次加载时,过滤器开始工作,但是当我单击按钮时,它进入
selectCategory()
函数并更改selectedCategory
变量的值(它甚至不是$scope
属性,而只是一个变量)。 After that angular filters the ng-repeat
again. 之后,角度再次过滤
ng-repeat
。 I want to understand why ? 我想了解为什么? I understand that my variable
selectedCategory
is used in categoryFilterFn
function (which is filter), but does it mean that every change of it will cause new rendering ? 我知道我的变量
selectedCategory
在categoryFilterFn
函数(它是filter)中使用了,但这是否意味着它的每次更改都会引起新的渲染?
In angularjs in every change detection filter gets executed. 在angularjs中,每个更改检测过滤器都会执行。 When you are invoking
$scope.selectCategory
function angular internally run the digest cycle and firing events to identify the changes happening in the scope. 调用
$scope.selectCategory
函数时,在内部angular运行摘要循环和触发事件以识别作用域中发生的更改。 So each digest cycle or change detection filters are gonna fire again. 因此,每个摘要循环或更改检测过滤器都会再次触发。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.