javascript - Angularjs factory inject error - Stack Overflow

I'm using angularJS and when I'm injecting a Factory I get error:app.js :angular.module('

I'm using angularJS and when I'm injecting a Factory I get error:

app.js :

angular.module('myapp', [])

myfactory.js :

angular.module('myapp', [])
.factory('httpService', function($http, $timeout) {

});

Controller: test.js :

angular.module('myapp', [])
.controller('test',  function($scope, $timeout, $sce, $http, httpService) {

  $scope.submit = function() {
  }
});

When I add httpService I get error. Everything seems to be right, I even use this factory in all projects. Error:

angular.min.js:92 Error: [$injector:unpr] .2.25/$injector/unpr?p0=httpServiceProvider%20%3C-%20httpService
at Error (native)
at http://localhost:81/chah/assets/js/angularjs/angular.min.js:6:450
at http://localhost:81/chah/assets/js/angularjs/angular.min.js:36:202
at Object.c [as get] (http://localhost:81/chah/assets/js/angularjs/angular.min.js:34:305)
at http://localhost:81/chah/assets/js/angularjs/angular.min.js:36:270
at c (http://localhost:81/chah/assets/js/angularjs/angular.min.js:34:305)
at d (http://localhost:81/chah/assets/js/angularjs/angular.min.js:35:6)
at Object.instantiate (http://localhost:81/chah/assets/js/angularjs/angular.min.js:35:165)
at http://localhost:81/chah/assets/js/angularjs/angular.min.js:67:419
at http://localhost:81/chah/assets/js/angularjs/angular.min.js:54:25

I'm using angularJS and when I'm injecting a Factory I get error:

app.js :

angular.module('myapp', [])

myfactory.js :

angular.module('myapp', [])
.factory('httpService', function($http, $timeout) {

});

Controller: test.js :

angular.module('myapp', [])
.controller('test',  function($scope, $timeout, $sce, $http, httpService) {

  $scope.submit = function() {
  }
});

When I add httpService I get error. Everything seems to be right, I even use this factory in all projects. Error:

angular.min.js:92 Error: [$injector:unpr] http://errors.angularjs/1.2.25/$injector/unpr?p0=httpServiceProvider%20%3C-%20httpService
at Error (native)
at http://localhost:81/chah/assets/js/angularjs/angular.min.js:6:450
at http://localhost:81/chah/assets/js/angularjs/angular.min.js:36:202
at Object.c [as get] (http://localhost:81/chah/assets/js/angularjs/angular.min.js:34:305)
at http://localhost:81/chah/assets/js/angularjs/angular.min.js:36:270
at c (http://localhost:81/chah/assets/js/angularjs/angular.min.js:34:305)
at d (http://localhost:81/chah/assets/js/angularjs/angular.min.js:35:6)
at Object.instantiate (http://localhost:81/chah/assets/js/angularjs/angular.min.js:35:165)
at http://localhost:81/chah/assets/js/angularjs/angular.min.js:67:419
at http://localhost:81/chah/assets/js/angularjs/angular.min.js:54:25
Share Improve this question edited May 19, 2016 at 9:18 Vahid Najafi asked May 19, 2016 at 8:51 Vahid NajafiVahid Najafi 5,30311 gold badges51 silver badges95 bronze badges 2
  • which version of Angular your using ?. – Shushanth Pallegar Commented May 19, 2016 at 9:01
  • @shushanthp I tested in both angular 1.2.25 and 1.3.9 – Vahid Najafi Commented May 19, 2016 at 9:05
Add a ment  | 

4 Answers 4

Reset to default 6

Check the link in your error (https://docs.angularjs/error/$injector/unpr?p0=httpServiceProvider%20%3C-%20httpService):

You create module multiple times:

angular.module('myapp', [])

You should do it once. Then use without []

angular.module('myapp').factory ...
angular.module('myapp').controller ...

The reason for the error is because in the creation of the httpService and the controller you have used the setter i.e. angular.module('myapp', []) syntax for the module and not the getter syntax. angular.module('myapp'). Angular requires us to define a module only once, thus the subsequent redefining causes the error.

So in app.js, define the module:

angular.module('myapp', []) ;

In myfactory.js use the getter Syntax by removing the , []:

angular.module('myapp')
.factory('httpService', function($http, $timeout) {
});

And in test.js:

angular.module('myapp')
.controller('test',  function($scope, $timeout, $sce, $http,  httpService) {

$scope.submit = function() {
}
});

Here is a link to the docs

yes,

what you are doing is re-creating your app

what you need to do is define it once and continue using that instance

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

app.factory('httpService', function($http, $timeout) {

});

app.controller('test',  function($scope, $timeout, $sce, $http, httpService) {

  $scope.submit = function() {
  }
});

or if you want to retrieve your app the syntax is angular.module('myapp') this returns 'myapp', but adding [], tells angular to create an app and not fetch it

Code should look like below:

angular.module('myapp', [])
.factory('httpService', function($http, $timeout) {

});
.controller('test',  function($scope, $timeout, $sce, $http, httpService) {

  $scope.submit = function() {
  }
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744244470a4564859.html

相关推荐

  • javascript - Angularjs factory inject error - Stack Overflow

    I'm using angularJS and when I'm injecting a Factory I get error:app.js :angular.module('

    8天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信