qdave
qdave

Reputation: 11

Cant inject angular $filter service inside a factory

I'm trying to create a factory using angularjs, but when i try to inject $filter service in my factory i got this strange error:

Error: [$injector:unpr] Unknown provider: undefinedFilterProvider <- undefinedFilter <- myfact http://errors.angularjs.org/1.3.17/$injector/unpr?p0=undefinedFilterProvider%20%3C-%20undefinedFilter%20%3C-%20myfact at REGEX_STRING_REGEXP (angular.js:63) at angular.js:4031 at Object.getService [as get] (angular.js:4178) at angular.js:4036 at Object.getService [as get] (angular.js:4178) at $get (angular.js:16724) at Object.myfact (app.js:4) at Object.invoke (angular.js:4219) at Object.enforcedReturnValue [as $get] (angular.js:4072) at Object.invoke (angular.js:4219)

In my application I have many controllers that use the $filter service and I've never seen this error before.

Can someone give me a little help about this?

<!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="[email protected]" 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

Upvotes: 1

Views: 790

Answers (1)

James
James

Reputation: 3239

You've got an error in your code within your anonymous function that you assign to myfact. You write filter = $filter(filter) but the parameter filter that you pass into $filter is undefined.

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

Upvotes: 1

Related Questions