0

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?

Community
  • 1
  • 1
MoonMoon
  • 23
  • 4
  • 1
    it does somewhat make sense @Claies ... if OP names the argument the same as the injected service they lose scope inside function for accessing the service – charlietfl Oct 13 '15 at 21:21
  • @MonMoon ... your problem is related to javascript closures. When you have a local variable inside function with same name as a variable outside the function ... you can't access the outer one – charlietfl Oct 13 '15 at 21:22
  • You could have just named it `baloney` and it would have worked too. `function(id, baloney) {`. The problem is by naming it `activeMovie`, it takes place of the service injected to the controller entirely within that inner context. – Kevin B Oct 13 '15 at 21:23
  • It would appear your confusing scoping of your controllers. I presume with the use of `this` in your controller your using `controller as` in your view. Anyhow it all seems to work according to this fiddle: http://jsfiddle.net/60smz0hy/. However there is one thing to note. Why are you passing your custom service into your methods? There is no need to do that. Your injected services are available everywhere in your controller. I imagine your accidentally over-writing your services somewhere. There is no need to wrap things in closures, also you should move your $http to a service. – ste2425 Oct 13 '15 at 21:23
  • 1
    I basically figured out what the OP was talking about as I was re-reading the first comment I added. The question and the premise here is just confused. – Claies Oct 13 '15 at 21:24
  • thanks folks! Okay, so I don't have to commit the service 'activeController' to the functions inside the controller, when I make it available with the dependency injection, right? Works fine with that! – MoonMoon Oct 15 '15 at 06:44

0 Answers0