1

I have a service where I pass some data on click what works great, but now I wanna pull that data from the service and add it to a scope in another controller and just log everytime there is something added with the $watch function.

My service:

app.service('infoService', function() {
    var currentInfo = [];
    return this.method = function(data){
        return currentInfo = data;
    };
});

Where I try to pull the data

app.controller('clickController', function($scope, infoService) {
    $scope.data = infoService.method();
    $scope.$watch('data', function(newVal, oldVal) {
        console.log(newVal, oldVal);
    });
});

The only thing I currently see the last hour are errors and errors.

6
  • Can you be specific about the errors that you are seeing? Commented Nov 30, 2015 at 9:33
  • create plnkr to replicate this problem! Commented Nov 30, 2015 at 9:35
  • yes ofcourse, it keeps saying that the method is not a function, when I call it in the controller. If I log the data in the service there are no problems whatsover. Commented Nov 30, 2015 at 9:35
  • how could you log the data in the service when you can't call that method in your controller? Commented Nov 30, 2015 at 9:57
  • I dont know, I just update the service on a click with new data. and wanna pul that updated data out the service. Commented Nov 30, 2015 at 10:01

2 Answers 2

1

Can you remove the return statement from your service

this.method = function(data){
    return currentInfo = data;
};

then you should be able to call function in your service

hope this helps.

Sign up to request clarification or add additional context in comments.

1 Comment

Nope, thought also. But already tried that, doesn't do anything.
1

Try putting the watch on the return value of the service, like this:

$scope.$watch(function () { return infoService.method(); }, function(newVal, oldVal) {
        console.log(newVal, oldVal);
    });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.