gpt4 book ai didi

javascript - 无法在一个 Angular Controller 中使用多个服务

转载 作者:行者123 更新时间:2023-12-02 15:21:05 24 4
gpt4 key购买 nike

我想在一个 Controller 中使用 2 个服务,但不知何故第二个服务不起作用:

第一个 Controller (可以工作):

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>

为什么会发生这种情况,该服务的结构与第一个服务相同,但仍然不起作用。

挑战工厂:

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;
}
]);

最佳答案

好的,现在我明白你做了什么

此代码将创建新模块:

angular.module('services', [

])

如果你想添加到同一个模块,你应该这样做:

angular.module('services').service(...)

您创建了两个同名的模块并覆盖了您的模块

关于javascript - 无法在一个 Angular Controller 中使用多个服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34050471/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com