AngularJS - 过滤器不起作用

AngularJS - filter not working

本文关键字:不起作用 过滤器 AngularJS      更新时间:2023-10-12

我的过滤器有问题。我有这样的文件: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>