Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angularJS ng-model input type number to rootScope not updating

Tags:

angularjs

I have an input type number set to

<input type="number" ng-model="inputModel"/>

Where inputModel is $rootScope.inputModel. Every time I change the input box the value does not persist onto the $rootScope. Is it not possible to bind an input box to a $rootScope? what am I missing here?
I basically have another controller that performs calculations on given $rootScope and those calculations change depending on what the value of the input box is.
An help is much appreciated

Thanks

like image 822
climboid Avatar asked Mar 09 '13 00:03

climboid


2 Answers

See this question - you can use the $root property on the scope and the binding would be

<input type="number" ng-model="$root.inputModel"/>

This will bind directly on the root scope without the need to explicitly assign it in the controller.

like image 141
Boris Avatar answered Oct 10 '22 02:10

Boris


As others pointed out, it is a prototypical inheritance problem. Your input model is generated in the current scope, not the rootScope.

Always use "." in your views. This will work:

rootScope.fields = {
    inputModel: ''
}

and

<input type="number" ng-model="fields.inputModel"/>
like image 25
Vilmos Ioo Avatar answered Oct 10 '22 02:10

Vilmos Ioo