简体   繁体   English

在angularjs的ng-repeat之外,作用域不起作用

[英]scope is not working outside ng-repeat in angularjs

HTML 的HTML

<ul>
    <li ng-repeat="obj in strA">
       obj.str = {{obj}}
       <input ng-model="obj" />
    </li>
</ul>
obj in <code>{{ strA.a1 }}</code> 
obj in <code>{{ strA.a2 }}</code>

Controller 控制者

$scope.strA = {
  a1: "abc",
  a2: "bcd"
};

When I change the value in text box, obj inside ng-repeat changes. 当我在文本框中更改值时,ng-repeat内的obj也会更改。 but value wrapped by 但价值包裹 don't change 不要改变

Here is the codepen : http://codepen.io/anon/pen/vLyrxm 这是codepen: http ://codepen.io/anon/pen/vLyrxm
Why is it so? 为什么会这样呢? as per my knowledge, both the value should be change 据我所知,两者的价值都应该改变

ngRepeat creates new child scope. ngRepeat创建新的子范围。 It means that in each repeated li there is a new scope and with ngMOdel you are binding to this local scope. 这意味着在每个重复的li都有一个新作用域,而使用ngMOdel则绑定到该本地作用域。 You could do something like this instead: 您可以改为执行以下操作:

<li ng-repeat="(key, value) in strA">
    obj.str = {{value}}
    <input ng-model="strA[key]" />
</li>

Demo: http://codepen.io/anon/pen/adBKVd?editors=101 演示: http //codepen.io/anon/pen/adBKVd?editors = 101

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM