I want to get a service to story values, that should be available at some controllers, taking regard of this post: AngularJS: How can I pass variables between controllers?
Everything fine. But: I want the service to be available at deeper funtions of controllers. Here is my example:
app.service('activeMovie', function() {
var movie = {
rating: 100
};
return {
getData: function() {
return movie;
},
setRating: function(value) {
movie.rating = value;
console.log("neues Rating" + movie.rating);
},
setData: function(pData) {
movie = pData;
}
};
});
and this are the controllers:
app.controller('MoviesController', function($http, $sce, activeMovie) {
var refresh = function(activeMovie) {
// ...
console.log("Test Service at refresh:" + activeMovie.getData().rating); // WORKS
};
refresh(activeMovie);
this.setActive = function(id, $activeMovie) {
$http.get('movies/'+id).success(function(response) {
// ...
});
console.log("Test Service at setActive:" + activeMovie.getData().rating); // WORKS only with the $-sign in the header
};
}
So it works how I did this.. If I dont use the $-sign in the second function, angularJS throws an error. Obviously I don't have to pass the parameter 'activeMovie' to the funtion when using the $-sign.. but why?