1

I have the AngularJS module declaration as below. Here I have declared value AppModel. I wanted to write code which will be triggered when AppModel.currentTable is changed, How can I watch for this value change ?

angular.module('MIIApp', ['MIIServices']).
value("AppModel",{
    currentTable:"TESTME-h",
    currentUser:null
});

Any help!!!.

2
  • I found a solution, I have attached the "watcher" like below, Let me know if there is a better way run(function($rootScope, AppModel) { $rootScope.AppModel = AppModel; $rootScope.$watch("AppModel.currentTable", function(){ alert("currentable chagned : "+AppModel.currentTable); }); }); Commented Oct 9, 2012 at 15:46
  • Its not very consistent, some times this event is not fired at all, I tried updating the model value from a 'directive' but the event is not fired, But If I try to update the value from a 'controller' it works, Any idea why ? Commented Oct 13, 2012 at 8:41

1 Answer 1

1

I don’t think you should be doing this. As you can see here a value is intended to be a constant:

value(name, value)

A short hand for configuring services if the $get method is a constant.

In AngularJS a service is a singleton and a value is the most simple of the possible services, when called returns always the same. I don’t know what you need, but could it be you are confusing models with services?

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

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.