I am trying to use id between controllers in angular
I created the service as follows:
app.factory("idService", function() {
var id;
addId = function(id) {
id = id;
};
getId = function() {
return id;
};
});
In my controller, I am trying to use this service as follows:
app.controller('photoFormController', ['$scope', '$http', 'idService' , function($scope, $http, idService) {
$scope.id = idService.getId();
}]);
I get an error, cannot call the undefined method, it is obvious that I entered the service incorrectly. Can anyone help?
EDIT:
Based on the solution below, the service no longer generates errors, however, I can’t get the identifier back, I see that it is installed from one controller, however, when it is extracted, it remains undefined:
app.factory("idService", function() {
var id;
addId = function(id) {
id = id;
console.log("added id of: " + id);
};
getId = function() {
console.log("trying to return : " + id);
return id;
};
return {
addId: addId,
getId: getId
};
});
source
share