angularjs 필터에 인수 전달
임의의 이름으로 필터링 할 수 있도록 필터 함수에 인수를 전달할 수 있습니까?
같은 것
$scope.weDontLike = function(item, name) {
console.log(arguments);
return item.name != name;
};
실제로 angular의 기본 '필터'필터를 사용하고 여전히 사용자 지정 필터에 인수를 전달할 수있는 또 다른 (아마도 더 나은 솔루션)이 있습니다.
다음 코드를 고려하십시오.
<div ng-repeat="group in groups">
<li ng-repeat="friend in friends | filter:weDontLike(group.enemy.name)">
<span>{{friend.name}}</span>
<li>
</div>
이 작업을 수행하려면 필터를 다음과 같이 정의하면됩니다.
$scope.weDontLike = function(name) {
return function(friend) {
return friend.name != name;
}
}
여기에서 볼 수 있듯이 weDontLike는 실제로 범위에 매개 변수가있는 다른 함수와 필터에서 가져온 원래 항목을 반환합니다.
이 작업을 수행 할 수 있다는 사실을 깨닫는 데 2 일이 걸렸습니다. 아직이 솔루션을 본 적이 없습니다.
체크 아웃 angular.js 필터의 역 극성은 당신이 필터와 다른 유용한 작업이 사용할 수있는 방법을 볼 수 있습니다.
내가 이해 한 바에 따르면 필터 함수에 인수를 전달할 수 없습니다 ( '필터'필터를 사용할 때). 당신이해야 할 일은 다음과 같이 사용자 정의 필터를 작성하는 것입니다.
.filter('weDontLike', function(){
return function(items, name){
var arrayToReturn = [];
for (var i=0; i<items.length; i++){
if (items[i].name != name) {
arrayToReturn.push(items[i]);
}
}
return arrayToReturn;
};
작동하는 jsFiddle은 다음과 같습니다. http://jsfiddle.net/pkozlowski_opensource/myr4a/1/
사용자 지정 필터를 작성하지 않는 다른 간단한 대안은 범위에서 필터링 할 이름을 저장 한 다음 다음과 같이 작성하는 것입니다.
$scope.weDontLike = function(item) {
return item.name != $scope.name;
};
실제로 매개 변수 ( http://docs.angularjs.org/api/ng.filter:filter )를 전달할 수 있으며이를위한 사용자 지정 함수가 필요하지 않습니다. HTML을 아래와 같이 다시 작성하면 작동합니다.
<div ng:app>
<div ng-controller="HelloCntl">
<ul>
<li ng-repeat="friend in friends | filter:{name:'!Adam'}">
<span>{{friend.name}}</span>
<span>{{friend.phone}}</span>
</li>
</ul>
</div>
</div>
템플릿에서 간단하게 할 수 있습니다.
<span ng-cloak>{{amount |firstFiler:'firstArgument':'secondArgument' }}</span>
필터에서
angular.module("app")
.filter("firstFiler",function(){
console.log("filter loads");
return function(items, firstArgument,secondArgument){
console.log("item is ",items); // it is value upon which you have to filter
console.log("firstArgument is ",firstArgument);
console.log("secondArgument ",secondArgument);
return "hello";
}
});
Extending on pkozlowski.opensource's answer and using javascript array's
builtin filter method a prettified solution could be this:
.filter('weDontLike', function(){
return function(items, name){
return items.filter(function(item) {
return item.name != name;
});
};
});
Here's the jsfiddle link.
More on Array filter here.
You can pass multiple arguments to angular filter !
Defining my angular app and and an app level variable -
var app = angular.module('filterApp',[]);
app.value('test_obj', {'TEST' : 'test be check se'});
Your Filter will be like :-
app.filter('testFilter', [ 'test_obj', function(test_obj) {
function test_filter_function(key, dynamic_data) {
if(dynamic_data){
var temp = test_obj[key];
for(var property in dynamic_data){
temp = temp.replace(property, dynamic_data[property]);
}
return temp;
}
else{
return test_obj[key] || key;
}
}
test_filter_function.$stateful = true;
return test_filter_function;
}]);
And from HTML you will send data like :-
<span ng-bind="'TEST' | testFilter: { 'be': val, 'se': value2 }"></span>
Here I am sending a JSON object to the filter. You can also send any kind of data like string or number.
also you can pass dynamic number of arguments to filter , in that case you have to use arguments to get those arguments.
For a working demo go here - passing multiple arguments to angular filter
참고URL : https://stackoverflow.com/questions/11753321/passing-arguments-to-angularjs-filters
'IT박스' 카테고리의 다른 글
문자열의 첫 문자 만 대문자로하고 나머지는 그대로 두시겠습니까? (0) | 2020.08.21 |
---|---|
Java RegEx는 대소 문자를 구분하지 않습니까? (0) | 2020.08.21 |
입력에 대해 프로그래밍 방식으로 onchange 이벤트를 강제하는 방법은 무엇입니까? (0) | 2020.08.20 |
다른 장치 방향에서 UICollectionViewCell 크기 변경 (0) | 2020.08.20 |
notifyDatasetChanged () 후 RecyclerView 깜박임 (0) | 2020.08.20 |