내가로부터 각도 사용자 정의 필터의 예를 시도하고있다 : 내 버전처럼 보인다 https://scotch.io/tutorials/building-custom-angularjs-filters#filters-that-actually-filter :AngularJS와 사용자 정의 필터는 2 회
<!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>
은 그 일부를 CONSOLE.LOG에서 보인다 이유 맞춤 필터 함수 staticLanguage는 두 번 호출되지만 코드 자체에서 한 번만 호출됩니다. ng-repeat = "x in example1 | staticLanguage"
누구나 이유가 무엇입니까?
추 신 : "더티 검사"가 내 질문과 관련이있는 것이 무엇인지 아직 알지 못했습니다 ... 카운터 변수를 제거하고 일부 console.log ("text")를 staticLanguage 함수는 여전히 두 번 호출됩니다
그렇다면 그것은 단지 staticLanguage 일톤를 실행합니다 예? – BlackRaider