Reputation: 89
There is a simple example, what I want achieve, is when the text field is changed, both models should change. Is this possible without using $watch?
Upvotes: 1
Views: 913
Reputation: 19578
Alternatively, you can bind the root object, like in this example.
So your controllers do this:
Controller "first":
$scope.testModel = Data
Controller "second":
$scope.testModel = Data
Now, you use this as <input ng-model="testModel.test_var"/>
and in both views you use this:
<p>{{testModel.test_var}}</p>
Upvotes: 0
Reputation: 26841
You just need to make the object pass by reference instead of passing the string value..
See this solution
http://codepen.io/anon/pen/Dczmp
<div ng-controller="first">
<input type="text" ng-model="testModel.test_var" />
<p>{{testModel.test_var}}</p>
</div>
<div ng-controller="second">
<p>{{testModel.test_var}}</p>
</div>
In your controllers :
$scope.testModel = Data
instead of
$scope.testModel = Data.test_var
Upvotes: 5