我使用php后端开发Angularjs应用程序。我得到了这样的错误
Error: [$injector:unpr] http://errors.angularjs.org/1.2.25/$injector/unpr?p0=constantsProvider%20%3C-onstants%20%3C-%20userService
at http://localhost/Security/libs/js/angular.min.js:6:450
at http://localhost/Security/libs/js/angular.min.js:36:202
at Object.c [as get] (http://localhost/Security/libs/js/angular.min.js:34:305)
at http://localhost/Security/libs/js/angular.min.js:36:270
at c (http://localhost/Security/libs/js/angular.min.js:34:305)
at d (http://localhost/Security/libs/js/angular.min.js:35:6)
at Object.instantiate (http://localhost/Security/libs/js/angular.min.js:35:165)
at Object.<anonymous> (http://localhost/Security/libs/js/angular.min.js:35:435)
at Object.d [as invoke] (http://localhost/Security/libs/js/angular.min.js:35:36)
at http://localhost/Security/libs/js/angular.min.js:36:288
和我的控制器
(function () {
'use strict';
angular.module('smss').controller('loginController', ['$scope','userService','$http', loginController]);
function loginController($scope,userService, $http) {
$scope.data = {email: '', password: ''};
// login function
$scope.login = function (data) {
// user auth data
var authData = {
email: data.email,
password: data.password,
id: 1
}
userService.authUser(authData).success(function(){
}).error(function(){
$scope.data = {email : '',password : ''};
})
}
}
})();
和我的服务类如下
(function() {
"use strict";
angular.module('smss').service('userService', ['$http','constants', userService]);
function userService($http,constants) {
return {
checkUser: function(data){
return $http.post(constants.SERVER_URL+ '/api/checkUser',data);
},
newUser: function(data){
return $http.post(constants.SERVER_URL+ '/api/newUser',data);
},
authUser: function(data){
$http.post('api/authUser.php', data).success(function (data) {
console.log(data);
});
return $http.post(constants.SERVER_URL+ '/api/authUser',data);
}
};
}
})();
和常量
(function() {
"use strict";
angular.module('smss').constant('constants', {
SERVER_URL: 'http://localhost/security/'
})
})();
为什么,我得到这样的错误?我怎么解决?有 userService ,我想添加我的控制器,但我得到以下错误。
我想发送$ http post requst到我的php文件,但我试着这样做。
答案 0 :(得分:1)
你的问题是你没有正确地注入$ http。你忘记添加$ http。
angular.module('smss',[]);// You must need to declare module this way.
比,
angular.module('smss').controller('loginController', ['$scope','userService','$http', loginController]);
function loginController($scope,userService, $http) {}