AngularJS의 필터 내에서 매개 변수를 사용하는 방법은 무엇입니까?
ng-repeat로 일부 배열을 반복 할 때 필터에서 매개 변수를 사용하고 싶습니다.
예:
HTML 부분 :
<tr ng-repeat="user in users | filter:isActive">
자바 스크립트 부분 :
$scope.isActive = function(user) {
return user.active === "1";
};
하지만 다음과 같은 필터를 사용할 수 있기를 원합니다.
<tr ng-repeat="user in users | filter:isStatus('4')">
그러나 작동하지 않습니다. 어떻게 그렇게 할 수 있습니까?
업데이트 : 나는 정말 충분히 설명서를 보지 않았다하지만 당신은 확실히 사용할 수있는 것 같아요 필터 구문 (참조와 필터를 이 바이올린을 객체의 속성에 의해 필터) :
<tr ng-repeat="user in users | filter:{status:4}">
누군가에게 도움이 될 경우를 대비 한 원래 답변은 다음과 같습니다.
필터 필터를 사용하면 매개 변수를 전달할 수 없지만 최소한 두 가지 작업을 수행 할 수 있습니다.
1) 범위 변수에서 필터링하려는 데이터를 설정하고 this fiddle 과 같은 필터 함수에서 참조하십시오 .
자바 스크립트 :
$scope.status = 1;
$scope.users = [{name: 'first user', status: 1},
{name: 'second user', status: 2},
{name: 'third user', status: 3}];
$scope.isStatus = function(user){
return (user.status == $scope.status);
};
HTML :
<li ng-repeat="user in users | filter:isStatus">
또는
2) 이 바이올린 과 같은 매개 변수를받는 새 필터를 만듭니다 .
자바 스크립트 :
var myApp = angular.module('myApp', []);
myApp.filter('isStatus', function() {
return function(input, status) {
var out = [];
for (var i = 0; i < input.length; i++){
if(input[i].status == status)
out.push(input[i]);
}
return out;
};
});
HTML :
<li ng-repeat="user in users | isStatus:3">
이 필터는 status
배열의 객체에 재사용 가능성을 낮출 수 있는 속성 이 있다고 가정 하지만 이는 단지 예일뿐입니다. 필터 생성에 대한 자세한 내용은 이 문서 를 읽을 수 있습니다 .
이 질문은 Passing arguments to angularjs filters 과 거의 동일합니다 . 하지만 사람들이 볼 수 있도록 여기에 답변을 하나 더 게시하겠습니다.
실제로 각도의 기본 '필터'필터를 사용하고 여전히 사용자 정의 필터에 인수를 전달할 수있는 또 다른 (아마 더 나은 솔루션)이 있습니다.
다음 코드를 고려하십시오.
<li ng-repeat="user in users | filter:byStatusId(3)">
<span>{{user.name}}</span>
<li>
이 작업을 수행하려면 필터를 다음과 같이 정의하면됩니다.
$scope.byStatusId = function(statusId) {
return function(user) {
return user.status.id == statusId;
}
}
이 접근 방식은 개체 내부 깊숙이 중첩 된 값에 대한 비교를 수행 할 수 있기 때문에 더 다양합니다.
체크 아웃 angular.js 필터의 역 극성은 당신이 필터와 다른 유용한 작업이 사용할 수있는 방법을 볼 수 있습니다.
이는 약간 관련이 없을 수 있지만 사용자 지정 함수로 여러 필터를 적용하려는 경우 https://github.com/tak215/angular-filter-manager를 살펴 봐야합니다.
예 다음과 같은 학생 목록이 있습니다.
$scope.students = [
{ name: 'Hai', age: 25, gender: 'boy' },
{ name: 'Hai', age: 30, gender: 'girl' },
{ name: 'Ho', age: 25, gender: 'boy' },
{ name: 'Hoan', age: 40, gender: 'girl' },
{ name: 'Hieu', age: 25, gender: 'boy' }
];
성별을 기준으로 학생을 남학생으로 필터링하고 이름으로 필터링하고 싶습니다.
The first I create a function named "filterbyboy" as following:
$scope.filterbyboy = function (genderstr) {
if ((typeof $scope.search === 'undefined')||($scope.search === ''))
return (genderstr = "")
else
return (genderstr = "boy");
};
Explaination: if not filter name then display all students else filter by input name and gender as 'boy'
Here is full HTMLcode and demo How to use and operator in AngularJs example
If you have created an AngularJs custom filter, you can send multiple params to your filter.Here is usage in template
{{ variable | myFilter:arg1:arg2... }}
and if you use filter inside your controller here is how you can do that
angular.module('MyModule').controller('MyCtrl',function($scope, $filter){
$filter('MyFilter')(arg1, arg2, ...);
})
if you need more with examples and online demo, you can use this
AngularJs filters examples and demo
참고URL : https://stackoverflow.com/questions/11996857/how-to-use-parameters-within-the-filter-in-angularjs
'Program Club' 카테고리의 다른 글
파이썬에서 효율적인 날짜 범위 중복 계산? (0) | 2020.10.28 |
---|---|
실제 MySQL 쿼리 시간 측정 (0) | 2020.10.28 |
Scheme에서 eq ?, eqv ?, equal ?, =의 차이점은 무엇입니까? (0) | 2020.10.28 |
jQuery Chosen 드롭 다운 목록 지우기 및 새로 고침 (0) | 2020.10.28 |
HTTP 오류 429 (너무 많은 요청)를 피하는 방법 (0) | 2020.10.28 |