0
我想在一個控制器使用2個服務於一體的控制器使用多項服務,但不知何故,第二個服務無法正常工作:不能在角
首先控制器(工作):
angular.module('services', [
])
.service('userService', function ($http, $q, auth, userFactory) {
var user = null;
if(!user){
userFactory.getUser(auth.currentUser()).getValue(function(result){
user = result;
return result;
});
}
this.getUser = function() {
//console.log("user: ", user);
return user;
}
this.setUserChallenge = function(id) {
var newUser = user;
//console.log("newuser: ", newUser);
newUser.currentchallenge = id;
//console.log("newuser2: ", newUser);
user = newUser;
}
})
;
第二個服務:
angular.module('services', [
])
.service('suggestionsService', function ($http, auth, challengeFactory, userService) {
var suggestions = null;
if(!suggestions){
$scope.$watch(userService.getUser, function(getUser){
console.log(getUser);
if(getUser) {
challengeFactory.findManyChallengesById(getuser.challengessuggestions).getValue(function(challengesResponse) {
$scope.suggestions = challengesResponse.data;
});
}
});
/*challengeFactory.getChallenges().getValue(function(result){
suggestions = result;
return result;
});*/
}
this.getSuggestions = function() {
return suggestions;
}
})
;
我這樣引用它們:
angular.module('eva').controller('ChallengeCtrl', ['$scope', 'auth','$translate', 'challengeFactory', 'userFactory', 'userService', 'suggestionsService'
function($scope, auth, $translate, challengeFactory, userFactory, userService, suggestionsService) {
但我得到這個錯誤:
Error: [$injector:unpr] http://errors.angularjs.org/1.4.7/ $injector/unpr?p0=suggestionsServiceProvider%20%3C-uggestionsService%20%3C-%20ChallengeCtrl
我引用他們在我的index.html:
<script type="text/javascript" src="js/services/bootstrap.js"></script>
<script type="text/javascript" src="js/services/ChallengesService.js"></script>
<script type="text/javascript" src="js/services/SuggestionsService.js"></script>
<script type="text/javascript" src="js/services/UserService.js"></script>
<script type="text/javascript" src="js/app.js"></script>
這是怎麼回事,該服務具有相同的結構,第一個但仍不起作用。
ChallengeFactory:
angular.module('factories')
.factory('challengeFactory', ['$http', '$state', '$window',
function($http, $state, $window) {
var challengeFactory = {};
challengeFactory.startSeries = function(user){
return{
getValue: function(){
$http({
method: 'POST',
url:'http://groep6api.herokuapp.com/startuserseries',
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
transformRequest: function(obj) {
var str = [];
for(var p in obj)
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
return str.join("&");
},
data : {username: user}
});
}
}
};
challengeFactory.setsuggestions = function(user, suggestions){
return{
getValue: function(){
$http({
method: 'POST',
url:'http://groep6api.herokuapp.com/setsuggestions',
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
transformRequest: function(obj) {
var str = [];
for(var p in obj)
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
return str.join("&");
},
data : {username: user, challengessuggestions: JSON.stringify(suggestions)}
});
}
}
};
challengeFactory.getChallenges = function(){
return{
getValue: function(callback){
$http({
method: 'GET',
url:'http://groep6api.herokuapp.com/challenges'
}).then(function (result) {
callback(result.data);
});
}
}
};
challengeFactory.findChallengeById = function(id){
return{
getValue: function(callback){
$http({
method: 'POST',
url:'http://groep6api.herokuapp.com/findchallengebyid',
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
transformRequest: function(obj) {
var str = [];
for(var p in obj)
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
return str.join("&");
},
data : {_id: id}
}).then(function (result) {
callback(result);
});
}
}
};
challengeFactory.findManyChallengesById = function(challengesIds){
return{
getValue: function(callback){
$http({
method: 'POST',
url:'http://groep6api.herokuapp.com/findmanychallengesbyid',
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
transformRequest: function(obj) {
var str = [];
for(var p in obj)
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
return str.join("&");
},
data : {ids: JSON.stringify(challengesIds)}
}).then(function (result) {
callback(result);
});
}
}
}
challengeFactory.setUserChallenge = function(user, id){
return{
getValue: function(callback){
$http({
method: 'POST',
url:'http://groep6api.herokuapp.com/setuserchallenge',
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
transformRequest: function(obj) {
var str = [];
for(var p in obj)
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
return str.join("&");
},
data : {username: user, _id: id}
}).then(function (result) {
callback(result);
});
}
}
};
challengeFactory.completeChallenge = function(user){
return{
getValue: function(){
$http({
method: 'POST',
url:'http://groep6api.herokuapp.com/completecurrentchallenge',
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
transformRequest: function(obj) {
var str = [];
for(var p in obj)
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
return str.join("&");
},
data : {username: user}
}).then(function (result) {
});
}
}
};
challengeFactory.completeChallengeSeries = function(username){
return{
getValue: function(){
$http({
method: 'POST',
url:'http://groep6api.herokuapp.com/completechallengeseries',
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
transformRequest: function(obj) {
var str = [];
for(var p in obj)
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
return str.join("&");
},
data : {username: username}
}).then(function (result) {
});
}
}
};
challengeFactory.setRating = function(score, challenge, user){
$http({
method: 'POST',
url:'http://groep6api.herokuapp.com/setrating',
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
transformRequest: function(obj) {
var str = [];
for(var p in obj)
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
return str.join("&");
},
data : {user: user, challenge: challenge, score: score}
}).success(function (result) {
}).error(function(err){
console.log(err);
});
};
challengeFactory.getScore = function(user, challenge){
return{
getValue: function(callback){
$http({
method: 'POST',
url:'http://groep6api.herokuapp.com/getscore',
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
transformRequest: function(obj) {
var str = [];
for(var p in obj)
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
return str.join("&");
},
data : {user: user, challenge: challenge}
}).then(function (result) {
callback(result.data);
});
}
}
};
return challengeFactory;
}
]);
是你的第一個服務作品嗎? – azad
是的第一個作品 –
在引用它們之前,你確定服務是否存在?如果他們在不同的文件中首先包含服務文件在你的html中。 – moonlight