另一个UNKNOWN提供商Angularjs

时间:2017-07-10 18:11:31

标签: angularjs angular-services

好的,所以我是棱角分明的新手,我一直在网上寻找这个问题的答案。我有一个未知的提供程序错误,我无法找到正确的答案我已经尝试过多次回复从堆栈溢出和其他很多地方,但它没有帮助我找到它的正确答案。

我有模块。

var app = angular.module('app', ['ngRoute','app.student']).config(['$httpProvider', '$routeProvider', function ($httpProvider,$routeProvider) {
//Various code
}]);

这是我的控制器

angular.module('app.student', []).controller('StudentCtrl', ['StudentService', '$scope', '$http', function (StudentService, $scope, $http) {
 //Code 
 }]);

现在我的服务

angular.module('app.student', []).service('StudentService', ['$http', function ($http) {
// Some More Code
}]);

我还是这种语言的新手,我再次在Stackoverflow上尝试了多个答案,没有一个答案对我有用。所以我决定寻求一些帮助。感谢

P.S。如果您需要更多我的代码,请告诉我。

2 个答案:

答案 0 :(得分:1)

您正在创建app.student模块两次。

首先创建模块(然后创建控制器):

angular.module('app.student', []).controller('StudentCtrl', ['StudentService', '$scope', '$http', function (StudentService, $scope, $http) {
     //Code 
 }]);

然后一旦创建,只需检索它。不要传入一系列依赖项,否则你将再次创建它:

angular.module('app.student').service('StudentService', ['$http', function ($http) {
    // Some More Code
}]);

答案 1 :(得分:0)

您不需要将app.student作为依赖项,

var app = angular.module('app', ['ngRoute']).config(['$httpProvider', '$routeProvider', function ($httpProvider,$routeProvider) {
//Various code
}]);

你的控制器代码应该是,

angular.module('app').controller('StudentCtrl', ['StudentService', '$scope', '$http', function (StudentService, $scope, $http) {
 //Code 
 }]);

和服务应该是,

angular.module('app').service('StudentService', ['$http', function ($http) {
// Some More Code
}]);