无法在工厂内注入角度$ filter服务

时间:2015-08-06 14:02:56

标签: angularjs

我正在尝试使用angularjs创建一个工厂,但是当我尝试在我的工厂注入$ filter服务时,我遇到了这个奇怪的错误:

  

错误:[$ injector:unpr]未知提供者:undefinedFilterProvider< - undefinedFilter< - myfact   http://errors.angularjs.org/1.3.17/ $注射器/ unpr?P0 = undefinedFilterProvider%20%3 C-%20undefinedFilter%20%3 C-%20myfact       在REGEX_STRING_REGEXP(angular.js:63)       在angular.js:4031       at Object.getService [as get](angular.js:4178)       在angular.js:4036       at Object.getService [as get](angular.js:4178)       at $ get(angular.js:16724)       在Object.myfact(app.js:4)       at Object.invoke(angular.js:4219)       at Object.enforcedReturnValue [as $ get](angular.js:4072)       在Object.invoke(angular.js:4219)

在我的应用程序中,我有许多使用$ filter服务的控制器,我以前从未见过这个错误。

有人可以给我一些帮助吗?

<!DOCTYPE html>
<html ng-app="plunker">

  <head>
    <meta charset="utf-8" />
    <title>AngularJS Plunker</title>
    <script>document.write('<base href="' + document.location + '" />');</script>
    <link rel="stylesheet" href="style.css" />
    <script data-require="angular.js@1.3.x" src="https://code.angularjs.org/1.3.17/angular.js" data-semver="1.3.17"></script>
    <script src="app.js"></script>
  </head>

  <body ng-controller="MainCtrl">
    <p>Hello {{name}}!</p>
  </body>

</html>


var app = angular.module('plunker', []);
var myfact = function($filter){
    var factory = {},
        filter = $filter(filter);
    return factory;
}
myfact.$inject = ['$filter'];
app.factory('myfact',myfact);
app.controller('MainCtrl', function($scope, myfact) {
    $scope.name = 'World';
});

Plunker snippet

1 个答案:

答案 0 :(得分:1)

您在分配给myfact的匿名函数中的代码中出现错误。您编写了filter = $filter(filter),但传递给filter的参数$filter未定义。

var app = angular.module('plunker', []);
var myfact = function($filter){
    var factory = {},
        filter = $filter(filter);   // <<-------  your error is on this line
    return factory;
}