Why doesn't the input change the value outside of ng-repeat (and ng-switch)?

I assume that the problem with the scope is the reason that the input in this example can change the value next to it, but not another value. If so, how to connect the model to the correct area? If this is not a scale problem, what am I doing wrong?

<html ng-app> <head> <script type="text/javascript" src="angular-1.0.1.min.js"></script> <script type="text/javascript"> function ExampleCtrl($scope) { $scope.list = [ { name: "a" }, { name: "b" }, ]; $scope.value = 5; } </script> </head> <body ng-controller="ExampleCtrl"> <ul ng-repeat="item in list"> <li>{{ item.name }} <ng-switch on="item.name"> <span ng-switch-when="b"> <input type="number" ng-model="value" /> {{ value }} </span> </ng-switch> </li> </ul> value is {{ value }} </body> </html> 
+7
source share
2 answers

Another way is that you can access the parent scope using $ parent.

 <input type="number" ng-model="$parent.$parent.value" /> 

See an example: http://jsfiddle.net/7Hh2R/

+7
source

You are right as ngRepeat , and ngSwitch creates different areas. You can use the amazing AngularJS Batarang developer tool for Chrome to see different areas of action.

One change you can make to refer to the same value in code is a reference to an object property instead of a primitive type:

 $scope.value = { val: 5 }; 

and bind to:

 <input type="number" ng-model="value.val"/> {{ value.val }} 

I will be interested to know if there are ways to handle this using different areas.

+5
source

All Articles