我想获得故事值的服务,应该在某些控制器上提供,并考虑到这篇文章:AngularJS: How can I pass variables between controllers?
一切都很好。但是:我希望服务能够在更深层次的控制器功能中使用。这是我的例子:
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;
}
};
});
这是控制器:
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
};
}
所以我的工作原理如何...如果我不在第二个函数中使用$ -sign,angularJS会抛出一个错误。显然,我不必通过参数' activeMovie'使用$ -sign时的功能..但为什么?