-->

ng-repeat execute many times

2020-02-06 18:01发布

问题:

I have a little demo on jsfiddle :

HTML:

<input type="checkbox" id="chkUsr{{usr.Id}}" ng-model="usr.Checked">
{{usr.Name}} : {{usr.Checked}}
<input type="text" ng-model="research"/>
<div ng-repeat="entity in entities | filter:research | log">
Hello {{entity.id}}!

JavaScript:

app.filter('log', function() {
    return function(items) {
        console.log('yo');
        return items;
    };
});

The log filter is called when input change (even the checkbox).

How to change that and trigger log filter only when text input change ?

回答1:

That's because angular runs $digest and updates all the scope properties even if one variable in the scope changes. It is called "dirty checking".

Learn more about how angular works: http://www.sitepoint.com/understanding-angulars-apply-digest/