As mentioned in my other posts, i am still new to AngularJS and I am having quite a bit of difficulties understanding its' ins and outs.
I have reached a blocking point where I am trying to pass data between a controller which stores a list of objects and another where the user can edit individual objects within a different view.
I have tried using my own factory for it, however if i reference the service within the index.html file (it's a single page application) I get errors regarding the state of my NavigationController which does not use the service in question.
My Factory code is below:
var app = angular.module('MyAppName',[]);
app.factory('CharacterData', function() {
var data = {
Name: '',
Description: '',
TotalExperience: 0,
RemainingExperience: 0
}
return {
getName: function() {
return data.Name;
},
setName: function(name) {
data.Name = name;
},
getDescription: function() {
return data.Description;
},
setDescription: function(description) {
data.Description = description;
},
getTotalExperience: function() {
return data.TotalExperience;
},
setTotalExperience: function(totalxp) {
data.TotalExperience = totalxp;
},
getRemainingExperience: function() {
return data.RemainingExperience;
},
setRemainingExperience: function(exp) {
data.RemainingExperience = exp;
}
};
});
I have injected this factory in both controllers that need it (CharacterListController and CharacterEditController), code below:
(function(){
angular.module('MyAppName')
.controller('CharacterEditController', ['$scope', '$state', '$http', '$location', function ($scope, $state, $http, $location, CharacterData){
/**
*
*/
$scope.readCharacter = function() {
$scope.currentCharacter.name = CharacterData.getName();
$scope.currentCharacter.description = CharacterData.getDescription();
$scope.currentCharacter.totalExperience = CharacterData.getTotalExperience();
$scope.currentCharacter.remainingExperience = CharacterData.getRemainingExperience();
}
...
}]);
}());
and the listing controller from where the character in question is passed:
(function(){
angular.module('TimeWaste')
.controller('CharacterListController',['$scope', '$state', '$http','$location', function($scope, $state, $http, $location, CharacterData){
.... more functions ...
$scope.updateCurrentCharacter = function(req, res) {
CharacterData.setName($scope.character.name);
CharacterData.setDescription($scope.character.description);
CharacterData.setTotalExperience($scope.character.totalExperience);
CharacterData.setRemainingExperience($scope.character.remainingExperience);
$location.path('/edit-character');
}
}]);
}());
What I do not understand is what does the NavigationController have to do anything with passing data. I have referenced my service like so:
What I get when the service is added to index.html
I am not understanding exactly how it works or why this problem occurs. I've been following tutorials mostly for the things i have been doing but as I said I am still in my MEAN Stack/Angular ABCs
Any help would be appreciated.
-
CharacterData should also be listed in your dependencies injection with quotes: app.controller('CharacterEditController', ['$scope', '$state', '$http', '$location', 'CharacterData', function ($scope, $state, $http, $location, CharacterData){Paqman– Paqman2016年03月07日 14:16:12 +00:00Commented Mar 7, 2016 at 14:16
3 Answers 3
You didn't inject it properly:
['$scope', '$state', '$http', '$location', 'CharacterData', function ($scope, $state, $http, $location, CharacterData){}]
Comments
well you missed on listing the CharacterData in your dependencies array
['$scope', '$state', '$http', '$location', 'CharacterData', function ($scope, $state, $http, $location, CharacterData){ ...
1 Comment
Your controllers are not on the same module. CharacterListController is on TimeWaste module and CharacterEditController is on MyAppName, so does CharacterData.
Try to use the same module or add them to your app dependency.