在ng-repeat中修改角度范围内的对象

时间:2021-07-25 23:12:19

I'm creating a form in HTML using ng-repeat to generate the form elements from an object in the scope. I also use that object to generate other elements outside of the ng-repeat.

我正在使用ng-repeat在HTML中创建一个表单,以从作用域中的对象生成表单元素。我还使用该对象生成ng-repeat之外的其他元素。

A simplified example looks like this in HTML:

一个简化的示例在HTML中如下所示:

<div ng-app="App">
  <div ng-controller="Ctrl">
      <div class="block1">
          <form ng-repeat="(key, value) in test">
              <label>{{key}}</label>
              <input ng-model="value" />
              <p>{{value}}</p>
          </form>
      </div>
      <div class="block2">
        <p>
          {{test.a}}
        </p>
        <p>
            {{test.b}}
        </p>
      </div>
  </div>
</div>

and this in JS:

这在JS中:

angular.module('App', []);

function Ctrl($scope) {
    $scope.test = {
        a:"abc",
        b:"def"
    }
}

In this example, the text in block2 is set to the initial values of test.a and test.b. The input values and <p> values inside of the loop are also set to the initial value.

在此示例中,block2中的文本设置为test.a和test.b的初始值。循环内的输入值和

值也设置为初始值。

When I modify the values within the inputs, the <p> values inside of the ng-repeat block update correctly, but the <p> tags in block2 fail to update.

当我修改输入中的值时,ng-repeat块内的

值会正确更新,但block2中的

标记无法更新。

Why is this the behavior? Does ng-repeat create its own isolated scope? If so how can I get the controller level scope to update? Also, could somebody explain the thinking behind this behavior and any advantages it provides?

为什么这是行为? ng-repeat是否创建了自己的隔离范围?如果是这样,我怎样才能让控制器级别范围更新?此外,有人可以解释这种行为背后的想法及其提供的任何好处吗?

JSFiddle Showing the problem

JSFiddle显示问题

2 个解决方案

#1


23  

ng-repeat creates a child scope for each repeated item. As a result you are trying to pass a primitive to child scope which won't create a reference to parent. When you pass objects however, you pass the original object reference.

ng-repeat为每个重复项创建子范围。因此,您尝试将原语传递给子范围,而不会创建对父级的引用。但是,传递对象时,会传递原始对象引用。

From the mouth of one of the fathers of Angular:

从Angular的一位父亲的口中:

Always have a dot in ng-model

在ng-model中总是有一个点

This is a great video regarding Angular Best Practices given by Angular creator (2012/12/11). Go to minute 31 for well explained detail of this exact situation

这是关于Angular创作者(2012/12/11)给出的Angular Best Practices的精彩视频。转到第31分钟,详细了解这种情况

Modify data to array of objects:

将数据修改为对象数组:

$scope.test = [{ val:"abc",key:'a'}, {val:"def",key:'b'} ]

Then in repeater:

然后在转发器中:

<form ng-repeat="item in test">
  <label>{{item.key}}</label>
  <input ng-model="item.val" />
  <p>{{item.val}}</p>
</form>

DEMO

DEMO

#2


2  

try this:

尝试这个:

    angular.module('App', []);

function Ctrl($scope) {
    $scope.test = [
        {label:"a", value:"abc"},
        {label:"b", value:"def"}
    ]
}

and

<div ng-app="App">
  <div ng-controller="Ctrl">
      <div class="block1">
          <form ng-repeat="o in test">
              <label>{{o.label}}</label>
              <input ng-model="o.value" />
              <p>{{o.value}}</p>
          </form>
      </div>
      <div class="block2">
        <p>
          {{test[0].value}}
        </p>
        <p>
            {{test[1].value}}
        </p>
      </div>
  </div>
</div>

Angularjs uses the fact that objects are passed by reference. So, if you pass a object to a function and change the object inside the function, the object outside also changes. Look at this updated JSFiddle

Angularjs使用对象通过引用传递的事实。因此,如果将对象传递给函数并更改函数内的对象,则外部对象也会更改。看看这个更新的JSFiddle

#1


23  

ng-repeat creates a child scope for each repeated item. As a result you are trying to pass a primitive to child scope which won't create a reference to parent. When you pass objects however, you pass the original object reference.

ng-repeat为每个重复项创建子范围。因此,您尝试将原语传递给子范围,而不会创建对父级的引用。但是,传递对象时,会传递原始对象引用。

From the mouth of one of the fathers of Angular:

从Angular的一位父亲的口中:

Always have a dot in ng-model

在ng-model中总是有一个点

This is a great video regarding Angular Best Practices given by Angular creator (2012/12/11). Go to minute 31 for well explained detail of this exact situation

这是关于Angular创作者(2012/12/11)给出的Angular Best Practices的精彩视频。转到第31分钟,详细了解这种情况

Modify data to array of objects:

将数据修改为对象数组:

$scope.test = [{ val:"abc",key:'a'}, {val:"def",key:'b'} ]

Then in repeater:

然后在转发器中:

<form ng-repeat="item in test">
  <label>{{item.key}}</label>
  <input ng-model="item.val" />
  <p>{{item.val}}</p>
</form>

DEMO

DEMO

#2


2  

try this:

尝试这个:

    angular.module('App', []);

function Ctrl($scope) {
    $scope.test = [
        {label:"a", value:"abc"},
        {label:"b", value:"def"}
    ]
}

and

<div ng-app="App">
  <div ng-controller="Ctrl">
      <div class="block1">
          <form ng-repeat="o in test">
              <label>{{o.label}}</label>
              <input ng-model="o.value" />
              <p>{{o.value}}</p>
          </form>
      </div>
      <div class="block2">
        <p>
          {{test[0].value}}
        </p>
        <p>
            {{test[1].value}}
        </p>
      </div>
  </div>
</div>

Angularjs uses the fact that objects are passed by reference. So, if you pass a object to a function and change the object inside the function, the object outside also changes. Look at this updated JSFiddle

Angularjs使用对象通过引用传递的事实。因此,如果将对象传递给函数并更改函数内的对象,则外部对象也会更改。看看这个更新的JSFiddle