0

I'm trying out Angular custom filter example from: https://scotch.io/tutorials/building-custom-angularjs-filters#filters-that-actually-filter which in my version looks like:

<!DOCTYPE html>
<html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="demo" >

    <div>
      
                   <p><strong>Original:</strong></p>
                   <ul class="list">
                     <li ng-repeat="x in example1">{{ x.name }}</li>
                   </ul>
                   <p><strong>Static Language Filter:</strong></p>
                   <ul class="list">
                     <li ng-repeat="x in example1 | staticLanguage">{{x.name }}</li>
                   </ul>
               
      </div>

</div>

<script>
var app = angular.module('myApp', []);
var counter=0;

app.controller('demo', function($scope){  
  $scope.example1 = [
    {name: 'C#', type : 'static'},
    {name: 'PHP', type : 'dynamic'},
    {name: 'Go', type : 'static'},
    {name: 'JavaScript', type: 'dynamic'},
    {name: 'Rust', type: 'static'}
  ];
  
});


// Setup the filter
app.filter('staticLanguage', function() {  // Create the return function and set the required parameter name to **input**
  return function(input) {
    counter+=1;
    console.log(counter);    
    var out = [];
    // Using the angular.forEach method, go through the array of data and perform the operation of figuring out if the language is statically or dynamically typed.
    angular.forEach(input, function(input) {
      if (input.type === 'static') {       
        out.push(input);
      }      
    });    
    return out;

  };
});

</script>

</body>
</html>

It seems from console.log that for some reason custom filter function staticLanguage is called two times but from the code itself it is called only one time: ng-repeat="x in example1 | staticLanguage"

Anyone has any idea why?

P.S I've yet to figure out what does "dirty-checking" has to do with my question... if I remove counter variable and just put some console.log("text") in it's place staticLanguage function is still called two times

3 Answers 3

2

As far as I am aware this is due to AngularJS dirty-checking and has been asnwered elsewhere here. This is normal, have a read of the link.

Sign up to request clarification or add additional context in comments.

Comments

2

This is normal, angularjs uses a 'dirty-check' approach, so it needs to call all the filters to see if any changes exist. After this it detects that you have a change on one variable (the one that you typed) and then it re-executes all filters again to detect if it has other changes.

See the first answer of this question

How does data binding work in AngularJS?

1 Comment

so if i remove counter variable from it it will only run staticLanguage one time?
0

Well, I don't know if this will serve to you , but here's a snippet working that could be a possible solution for you:

var app = angular.module('app', []);

app.controller('mainCtrl', function($scope) {
  $scope.languages = [  
    {  
      "name":"C#",
      "type":"static"
    },
    {  
      "name":"PHP",
      "type":"dynamic"
    },
    {  
      "name":"Go",
      "type":"static"
    },
    {  
      "name":"JavaScript",
      "type":"dynamic"
    },
    {  
      "name":"Rust",
      "type":"static"
    }
  ];
  
  $scope.static_languages = $scope.languages.filter(x => x.type == 'static');
  $scope.dynamic_languages = $scope.languages.filter(x => x.type == 'dynamic');
});
<!DOCTYPE html>
<html ng-app="app">

<head>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.7/angular.min.js">
</script>
</head>
<body ng-controller="mainCtrl">
  <div>
<p><strong>All languages:</strong></p>
<ul class="list">
  <li ng-bind="language.name" ng-repeat="language in languages"></li>
</ul>
<p><strong>Static Languages Filter:</strong></p>
<ul class="list">
  <li ng-bind="language.name" ng-repeat="language in static_languages"></li>
</ul>
<p><strong>Dynamic Languages Filter:</strong></p>
<ul class="list">
  <li ng-bind="language.name" ng-repeat="language in dynamic_languages"></li>
</ul>
  </div>
</body>
</html>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.