Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在AngularJS中注册过滤器?_Angularjs - Fatal编程技术网

如何在AngularJS中注册过滤器?

如何在AngularJS中注册过滤器?,angularjs,Angularjs,我的模块是使用Typescript创建的: angular.module('admin', ['ngMessages']) .service('homeService', HomeService) .controller('adminHomeController', AdminHomeController) .controller('adminContentController', AdminContentController) .filter('sdate',

我的模块是使用Typescript创建的:

angular.module('admin', ['ngMessages'])
    .service('homeService', HomeService)
    .controller('adminHomeController', AdminHomeController)
    .controller('adminContentController', AdminContentController)
    .filter('sdate', function ($filter) {
        var angularDateFilter = $filter('date');
        return function (theDate) {
            return angularDateFilter(theDate, 'yyyy-MM-dd');
        }
    });
在我的HTML页面上,我引用了如下过滤器:

{{ row.createdDate | sdate }}
但它给了我一个错误:

Error: [$injector:unpr] Unknown provider: nProvider <- n <- sdateFilter
http://errors.angularjs.org/1.3.0-beta.8/$injector/unpr?p0=nProvider%20%3C-%20n%20%3C-<div data-ui-view="content" id="contentBlock" class="ng-scope">dateFilter
    at http://127.0.0.1:17316/Scripts/bundle/library.js:80:20
    at http://127.0.0.1:17316/Scripts/bundle/library.js:3837:27
    at Object.getService [as get] (http://127.0.0.1:17316/Scripts/bundle/library.js:3969:53)
    at http://127.0.0.1:17316/Scripts/bundle/library.js:3842:53
    at getService (http://127.0.0.1:17316/Scripts/bundle/library.js:3969:53)
    at Object.invoke (http://127.0.0.1:17316/Scripts/bundle/library.js:4001:25)
    at http://127.0.0.1:17316/Scripts/bundle/library.js:3843:45
    at Object.getService [as get] (http://127.0.0.1:17316/Scripts/bundle/library.js:3969:53)
    at null.$filter (http://127.0.0.1:17316/Scripts/bundle/library.js:14624:34)
    at Parser.filter (http://127.0.0.1:17316/Scripts/bundle/library.js:10627:27)

但我还是犯了同样的错误。我注册过滤器的方式有问题吗?

当您看到类似的情况时

Error: [$injector:unpr] Unknown provider: nProvider <- n <- sdateFilter
一旦开始使用数组表示法,就可以调用任何参数

榜样

    .filter('sdate', ['$filter',function (n) {
        var angularDateFilter = n('date');
        return function (theDate) {
            return angularDateFilter(theDate, 'yyyy-MM-dd');
        }
    }]);

另请参见:

非常感谢!我99%确信你的回答是正确的。我将测试你的建议,并期望接受答案。@SamanthaJ我花了很多时间试图修复这些错误。如果错误中的所有内容都只是一个字母,并且调用堆栈纯粹是角度的,那么这就变得困难多了。因此,让阵列注入成为一种强烈的习惯。这将为您以后节省大量时间。或者,在构建过程中加入一个
angular.module('admin', ['ngMessages'])
    .service('homeService', HomeService)
    .controller('adminHomeController', AdminHomeController)
    .controller('adminContentController', AdminContentController)
    .filter('sdate', ['$filter',function ($filter) {
        var angularDateFilter = $filter('date');
        return function (theDate) {
            return angularDateFilter(theDate, 'yyyy-MM-dd');
        }
    }]);
    .filter('sdate', ['$filter',function (n) {
        var angularDateFilter = n('date');
        return function (theDate) {
            return angularDateFilter(theDate, 'yyyy-MM-dd');
        }
    }]);