AngularJS - 过滤器不起作用
AngularJS - filter not working
我的过滤器有问题。我有这样的文件:PostController.js,PostService.js,PostFilers.js - 我的过滤器,应用程序.js - 路由。基本上,它是一个简单的博客应用程序,我有一个对象(由"postService"返回,带有博客文章,如下所示:
var posts = [
{ id: 1, date: '21-12-2014 14:51', title: 'Blog post article title', content: 'Some content', tags: [1, 2, 3, 5], category: 1 },
{ id: 2, date: '15-11-2014 9:14', title: 'Some kind of title', content: 'Another content', tags: [2, 3, 5], category: 1}
];
正如您在标签中看到的,我有标签 ID 数组,但我想显示标签名称而不是标签 ID,所以我决定创建一个过滤器:
(function () {
var app = angular.module('SimpleBlog');
app.filter("tagName", function () {
return function (input) {
return input + "_test";
};
});
});
这应该返回类似1_test、2_test等内容。我尝试像这样使用它:
{{ tag | filter: tagName }}
和
{{ tag | tagName }}
但这里的问题是这个过滤器不起作用。我没有看到任何错误,也没有看到带有"test"字符串的 Id。
我的控制器的内容如下所示:
(function () {
var app = angular.module('SimpleBlog');
app.controller('PostController', ['$scope', '$location', '$routeParams', 'postService',
function ($scope, $location, $routeParams, postsService) {
// something here
});
});
视图的网页:
<div ng-repeat="post in posts">
<h2>{{ post.id }} - {{ post.title }}</h2>
<h3>{{ post.date }}</h3>
<p>{{ post.content }}</p>
<p>Tags:
<span ng-repeat="tag in post.tags">
{{ tag | filter: tagName }}
</span>
</p>
</div>
我必须在某处注射一些东西吗?希望有人能帮忙。提前谢谢。:-)
邮政服务.js
(function () {
var app = angular.module('SimpleBlog');
app.service('postService', function () {
var posts = [
{ id: 1, date: '21-12-2014 14:51', title: 'Blog post article title', content: 'Some content', tags: [1, 2, 3, 5], category: 1 },
{ id: 2, date: '15-11-2014 9:14', title: 'Some kind of title', content: 'Another content', tags: [2, 3, 5], category: 1 }
];
return {
getPosts: function () {
return posts;
}
};
});
})();
后控制器.js
(function () {
var app = angular.module('SimpleBlog');
app.controller('PostController', ['$scope', '$location', 'postService',
function ($scope, $location, postsService) {
$scope.posts = postsService.getPosts();
}
]);
})();
后置过滤器.js
(function () {
var app = angular.module('SimpleBlog');
app.filter("tagName", function () {
return function (input) {
var result = input + "test";
return result;
};
});
})();
问题已解决为了防止变量应用程序污染全局范围,我将模块定义包含在 IIFE 中,但我忘记了最后的括号。因此未调用代码,因此未注册过滤器。
是:
(function () {
var app = angular.module('SimpleBlog');
});
应该是:
(function () {
var app = angular.module('SimpleBlog');
})();
试试这个:-
<html>
<head>
<title>Angular JS Views</title>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js"></script>
</head>
<body>
<div ng-app="SimpleBlog" ng-controller="PostController">
<div ng-repeat="post in posts">
<h2>{{ post.id }} - {{ post.title }}</h2>
<h3>{{ post.date }}</h3>
<p>{{ post.content }}</p>
<p>Tags:
<span ng-repeat="tag in post.tags">
{{ tag | tagName }}
</span>
</p>
</div>
</div>
<script>
angular.module('SimpleBlog', ['filters','services','controllers']);
</script>
<script src="http://abcd.com/postService.js"></script>
<script src="http://abcd.com/PostController.js"></script>
<script src="http://abcd.com/filter.js"></script>
</body>
</html>
后控.js
angular.module('controllers',['filters','services']).controller('PostController', ['$scope', '$location', 'postService',
function ($scope, $location, postsService) {
$scope.posts = postsService.getPosts();
console.log($scope.posts);
}
]);
邮政服务.js
angular.module('services',['filters','controllers']).service('postService', function () {
var posts = [
{ id: 1, date: '21-12-2014 14:51', title: 'Blog post article title', content: 'Some content', tags: [1, 2, 3, 5], category: 1 },
{ id: 2, date: '15-11-2014 9:14', title: 'Some kind of title', content: 'Another content', tags: [2, 3, 5], category: 1 }
];
return {
getPosts: function () {
return posts;
}
};
});
过滤器.js
angular.module('filters',['services','controllers']).filter("tagName", function () {
return function (input) {
var result = input + "test";
return result;
};
});
您可以在$scope内部定义过滤器函数,然后将其传递给过滤器喜欢过滤器:我的功能
尝试更改此设置,无需filter
关键字作为客户过滤器
从:
<span ng-repeat="tag in post.tags">
{{ tag | filter: tagName }}
</span>
自:
<span ng-repeat="tag in post.tags">
{{ tag | tagName }}
</span>
相关文章:
- 角度过滤器和ng点击不起作用
- 角度限制过滤器's的第二参数“s”;“开始”;在Angular 1.3中似乎不起作用
- Angular js过滤器在某些移动设备上不起作用
- jQuery dataTables基于类型的列筛选不起作用?Img alt过滤器/排序
- 简单顺序通过过滤器在 AngularJs 中不起作用
- gulp-main-bower文件正则表达式过滤器不起作用
- AngularJS - 过滤器不起作用
- EXT JS : 数据存储过滤器功能不起作用
- 反转过滤器在IE和野生动物园上不起作用
- 单击退格键 jQuery 过滤器不起作用
- 我的PIXASTIC“Lighten.js”过滤器Javascript和HTML5代码不起作用
- 单选按钮在嵌套的 ng 重复和过滤器中不起作用
- Java Jersey 1.8,添加CORS过滤器不起作用
- JavaScript 数据表过滤器选项不起作用
- 聚合物过滤器不起作用
- 过滤器搜索与 AngularJs 不起作用
- 为什么过滤器在角度的输入字段中不起作用
- 水平滚动网站上的公文包过滤器不起作用
- ng angularjs中的重复过滤器数据不起作用
- select标签中的Mixitup过滤器在谷歌chrome和IE中不起作用