为什么这个AngularJS自定义过滤器会出错?

时间:2015-01-05 14:53:57

标签: angularjs filter

我正在努力让这个自定义过滤器工作,并将其缩减为简单列表形式。

以下代码应该返回“Message:test”而不是返回以下错误。

我需要更改哪些内容,以便自定义过滤器可以正确返回文本?

<html>
    <head>
        <style type="text/css">
        </style>
    </head>
    <body ng-app="mainModule" ng-controller="dataController">

        Message: {{message | capitalizeEveryWord}}

        <script src ="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script>
        <script>
                    var mainModule = angular.module('mainModule', []);
                    mainModule.filter('capitalizeEveryWord', function() {
                        return 'test';
                    });

                    function dataController($scope) {
                        $scope.message = 'This is a test.';
                    }
        </script>
    </body>
</html>

> Error: [$interpolate:interr]
> http://errors.angularjs.org/1.2.26/$interpolate/interr?p0=%0A%0A%20%20%20%20%20%20%20%20Message%3A%20%7B%7Bmessage%20%7C%20capitalizeEveryWord%7D%7D%0A%0A%20%20%20%20%20%20%20%20&p1=TypeError%3A%20c.apply%20is%20not%20a%20function
> C/<@http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js:6:450
> f/q@http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js:80:1
> Zd/this.$get</k.prototype.$digest@http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js:109:287
> Zd/this.$get</k.prototype.$apply@http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js:112:396
> fc/c/<@http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js:18:268
> d@http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js:35:34
> fc/c@http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js:18:176
> fc@http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js:18:387
> Xc@http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js:17:415
> @http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js:215:30
> a@http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js:145:67

1 个答案:

答案 0 :(得分:0)

我意识到我减少了太多,它必须返回功能,而不是文本,这有效:

            mainModule.filter('capitalizeEveryWord', function() {
                var capitalizeEveryWord = function(input) {
                    return 'test';
                }
                return capitalizeEveryWord;
            });