如何在 Angularjs 中创建过滤器?
How to create filter in Angularjs?
我有这个课程集:
[{ id: 1, courseId: 2, text: 'John' },
{ id: 2, courseId: 2, text: 'Willi' },
{ id: 3, courseId: 2, text: 'Inga' },
{ id: 4, courseId: 1, text: 'Jerry' },
{ id: 5, courseId: 1, text: 'Michael' },
{ id: 1, courseId: 3, text: 'John' },
{ id: 2, courseId: 3, text: 'Willi' },
{ id: 3, courseId: 4, text: 'Inga' },
{ id: 4, courseId: 5, text: 'Jerry' },
{ id: 5, courseId: 5, text: 'Michael' }]
我有这个 id 数组:
[{"id": 3},{"id": 2},{"id": 1}]
我需要按 ID 数组过滤课程数组(即只显示 courseId = 3,2,1 的文本课程):
ng-repeat="course in courses| customFilter: [{"id": 3},{"id": 2},{"id": 1}]"
我需要在 angularJS 中创建自定义过滤器,它将按 ID 数组过滤课程数组。
知道如何为此目的实现 customFilter 吗?
您可以创建自定义 filter
以便为您提供过滤后的值,过滤器应将元素数组作为过滤器数组。
标记
ng-repeat="course in courses| customFilter: [{"id": 3},{"id": 2},{"id": 1}]""
过滤器
app.filter('customFilter', function(){
return function(array, filterArray){
var ids = [];
angular.forEach(filterArray, function(val, index) {
ids.push(val.id);
}
return array.filter(function(value){
return ids.indexOf(value.id) !== -1;
});
}
})
我在 angularJs 项目中创建了一个过滤器。
在我的 angular 应用名称中是 angularApp。
var app = angular.module('angularApp', []); // This is your main angular app.
现在您要为解码创建过滤器 url。
app.filter('decodeURL', function() {
return function(text) {
if(text) {
return text.split(' ').join('-').toLowerCase().replace(/[^a-z0-9]+/g, '-');
}
}
});
以上代码是创建一个过滤器来解码url。我的过滤器名称是 'decodeURL' 。我们将在我的代码中使用 decodeURL 作为过滤器
就像你的 URL 是-
http://www.example.com/test1 test2 tes3
然后过滤使URL像这样-
http://www.example.com/test1-test2-tes3
如何在 html-
中使用此过滤器
<a ui-sref="{{business.category[0].categoryName.toLowerCase()}}Detail({id:business.id,title:(business.title | decodeURL)})"></a>
// 以上是angularjs中的状态路由
<a href="/coupon/{{coupon.id}}/{{coupon.title | decodeURL}}"
class="btn btn-warning show-btnhome show-button-margin">Show</a>
//以上代码为URL重定向
我有这个课程集:
[{ id: 1, courseId: 2, text: 'John' },
{ id: 2, courseId: 2, text: 'Willi' },
{ id: 3, courseId: 2, text: 'Inga' },
{ id: 4, courseId: 1, text: 'Jerry' },
{ id: 5, courseId: 1, text: 'Michael' },
{ id: 1, courseId: 3, text: 'John' },
{ id: 2, courseId: 3, text: 'Willi' },
{ id: 3, courseId: 4, text: 'Inga' },
{ id: 4, courseId: 5, text: 'Jerry' },
{ id: 5, courseId: 5, text: 'Michael' }]
我有这个 id 数组:
[{"id": 3},{"id": 2},{"id": 1}]
我需要按 ID 数组过滤课程数组(即只显示 courseId = 3,2,1 的文本课程):
ng-repeat="course in courses| customFilter: [{"id": 3},{"id": 2},{"id": 1}]"
我需要在 angularJS 中创建自定义过滤器,它将按 ID 数组过滤课程数组。
知道如何为此目的实现 customFilter 吗?
您可以创建自定义 filter
以便为您提供过滤后的值,过滤器应将元素数组作为过滤器数组。
标记
ng-repeat="course in courses| customFilter: [{"id": 3},{"id": 2},{"id": 1}]""
过滤器
app.filter('customFilter', function(){
return function(array, filterArray){
var ids = [];
angular.forEach(filterArray, function(val, index) {
ids.push(val.id);
}
return array.filter(function(value){
return ids.indexOf(value.id) !== -1;
});
}
})
我在 angularJs 项目中创建了一个过滤器。
在我的 angular 应用名称中是 angularApp。
var app = angular.module('angularApp', []); // This is your main angular app.
现在您要为解码创建过滤器 url。
app.filter('decodeURL', function() {
return function(text) {
if(text) {
return text.split(' ').join('-').toLowerCase().replace(/[^a-z0-9]+/g, '-');
}
}
});
以上代码是创建一个过滤器来解码url。我的过滤器名称是 'decodeURL' 。我们将在我的代码中使用 decodeURL 作为过滤器 就像你的 URL 是-
http://www.example.com/test1 test2 tes3
然后过滤使URL像这样-
http://www.example.com/test1-test2-tes3
如何在 html-
中使用此过滤器<a ui-sref="{{business.category[0].categoryName.toLowerCase()}}Detail({id:business.id,title:(business.title | decodeURL)})"></a>
// 以上是angularjs中的状态路由
<a href="/coupon/{{coupon.id}}/{{coupon.title | decodeURL}}"
class="btn btn-warning show-btnhome show-button-margin">Show</a>
//以上代码为URL重定向