私はからの角度カスタムフィルタの例をしようとしている:私のバージョンでのように見える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は2回呼び出されますが、コード自体からは1回だけ呼び出されます。ng-repeat = "x in example1 | staticLanguage"
誰でも何らかの理由がありますか?
PS「汚れチェック」は私の質問と何が関係しているのかまだ分かりません... カウンター変数を削除してconsole.log( "text")を静的な場所に置くとstaticLanguage関数はまだ2回呼び出されます
私はそれから、カウンタ変数を削除する場合はそれだけでstaticLanguage 1トンを実行しますイム? – BlackRaider