gpt4 book ai didi

javascript - AngularJS - 过滤器不起作用

转载 作者:行者123 更新时间:2023-11-28 07:36:52 25 4
gpt4 key购买 nike

我的过滤器有问题。我有这样的文件:PostController.js、PostService.js、PostFilers.js - 我的过滤器、app.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。

我的 Controller 的内容如下所示:

(function () {
var app = angular.module('SimpleBlog');
app.controller('PostController', ['$scope', '$location', '$routeParams', 'postService',
function ($scope, $location, $routeParams, postsService) {
// something here
});
});

View 的 HTML:

<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>

我必须在某处注入(inject)一些东西吗?希望有人能帮忙。提前致谢。 :-)

PostService.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;
}
};
});
})();

PostController.js

(function () {
var app = angular.module('SimpleBlog');
app.controller('PostController', ['$scope', '$location', 'postService',
function ($scope, $location, postsService) {
$scope.posts = postsService.getPosts();
}
]);
})();

PostFilters.js

(function () {
var app = angular.module('SimpleBlog');
app.filter("tagName", function () {
return function (input) {
var result = input + "test";
return result;
};
});
})();

问题已解决为了防止变量 app 污染全局范围,我将模块定义包含在 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>

PostControl.js

angular.module('controllers',['filters','services']).controller('PostController', ['$scope', '$location', 'postService',
function ($scope, $location, postsService) {
$scope.posts = postsService.getPosts();
console.log($scope.posts);
}
]);

postservice.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;
};
});

关于javascript - AngularJS - 过滤器不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28499794/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com