0

I had created a factory in angular but i am getting the following error

Unknown provider: 

Here is the factory:

app.factory("getFoo", function($scope){
    return {
       getCommi: function(val,id){
          var array = ["hello","world"];
          return array;
       }
     } 
});

The controller is:

app.controller('myCtrl', ['$scope','getFoo',function($scope,getFoo){
   $scope.myArr = getFoo.getCommi(4,1);
}])

What can I have to do for fix this? I just don't see any problem.

5
  • Have you injected your factory in the controller? Commented Apr 3, 2017 at 7:10
  • Have you added factory to your module (e.g. angular.module('app'))? Commented Apr 3, 2017 at 7:11
  • i have edit the controller look like. i didn't add factory to my module Commented Apr 3, 2017 at 7:15
  • 2
    do not inject $scope to getFoo Commented Apr 3, 2017 at 7:17
  • thanks artur grzesiak, I removed the $scope from the factory and it works! Commented Apr 3, 2017 at 7:22

4 Answers 4

1

no need of scope and your factory injection should be like below

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope, getFoo) {
   $scope.myArr = getFoo.getCommi(4,1);
  
});

app.factory('getFoo', function () {
  return {
       getCommi: function(val,id){
          var array = ["hello","world"];
          return array;
       }
     } 
});    
<!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.2.28/angular.js" data-semver="1.2.28"></script>
    <script src="app.js"></script>
  </head>

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

</html>

Sign up to request clarification or add additional context in comments.

Comments

0

you just need to remove $scope from factory.

working fiddle link

Comments

0

Please remove $scope from the factory.

var app = angular.module('myApp',[]);

app.factory("getFoo", function(){
    return {
       getCommi: function(val,id){
          var array = ["hello","world"];
          return array;
       }
    } 
});

app.controller('myCtrl', ['$scope','getFoo',function($scope,getFoo){
   $scope.myArr = getFoo.getCommi(4,1);

}])

Comments

0

Remove $scope from your factory and add factory "getFoo" as a dependency in your app's run module like this:

app.run(['$rootScope', '$location', 'getFoo', function($rootScope, $location, getFoo) {

}]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.