core
core

Reputation: 33059

Watching a value service value?

I have this in my Angular app:

myApp.value(mySetting, mySettingValue);

Is it possible to $watch() mySetting for changes? I've tried but can't get it to work.

This doesn't seem to do the trick:

$rootScope.$watch(function() {
    return mySetting;
, function(newSettingValue) {
    console.log(Date.now() + ' ' + newSettingValue);
});

Upvotes: 1

Views: 31

Answers (1)

Pankaj Parkar
Pankaj Parkar

Reputation: 136134

You missed to true property as a 3rd parameter inside your $watch, adding true will keep deep watch on object

Code

$rootScope.$watch(function() {
    return mySetting;
, function(newSettingValue) {
    console.log(Date.now() + ' ' + newSettingValue);
}, true);

Example Plunkr

Upvotes: 1

Related Questions