我有一个类似下面的服务:
var app = angular.module('app');
app.service('messagingService', function($http){
this.getMessages = function(offset, limit, successCallback, errorCallback){
// some stuff goes here
};
});
我包含此文件,然后我包含我的控制器文件。
我的控制器就像这样开始
function Inbox($scope, $http, messagingService) {
}
但是当我加载页面时,我收到以下错误:
错误:[$ injector:unpr] http://errors.angularjs.org/undefined/ $ injector / unpr?p0 = messagingServiceProvider%20%3C-%20messagingService 在错误() 在https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.min.js:6:453 在https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.min.js:32:320 at Object.c [as get](https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.min.js:29:461) 在https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.min.js:32:388 在c(https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.min.js:29:461) 在d(https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.min.js:30:130) at Object.Xb.instantiate(https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.min.js:31:284) at $ get(https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.min.js:61:304) 在https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.min.js:48:476
当我在与控制器相同的文件中使用我的服务代码时,它运行良好。我在这里错过了什么?感谢
答案 0 :(得分:1)
您需要将控制器连接到模块。
var app = angular.module('app');
app.service('messagingService', function($http){
this.getMessages = function(offset, limit, successCallback, errorCallback){
// some stuff goes here
};
});
app.controller('Inbox',['$scope', '$http', 'messagingService',function($scope, $http, messagingService){}])
答案 1 :(得分:1)
好的方法是将服务作为依赖注入例如:
var app = angular.module('ur_app_name')
app.service('messagingService', function($http){
this.getMessages = function(offset, limit, successCallback, errorCallback){
//some stuff goes here
};
app.controller('GreetingController', ['$scope','messagingService' function($scope,messagingService) {
//something here
})
答案 2 :(得分:0)
尝试以下方法:
var app = angular.module('app', ['app.services']);
然后在您的services.js
中var services = angular.module('app.services');
services('messagingService', function($http){....