简体   繁体   English

AngularJS:ng-repeat中的ng-model绑定

[英]AngularJS : ng-model binding in ng-repeat

I'm looping through a series of data and want to dynamically bind a model. 我正在遍历一系列数据,并希望动态绑定模型。 My problem is that when looping through elements, it seems as if Angular creates a new scope for each iteration, so the models are not the same in the three iterations. 我的问题是,遍历元素时,Angular似乎为每次迭代都创建了一个新作用域,因此三个迭代中的模型并不相同。

I've made a simplified example of my code that does not work; 我已经做了一个简化的代码示例,该代码无法正常工作;

http://jsfiddle.net/Fizk/uurL65e5/ http://jsfiddle.net/Fizk/uurL65e5/

<div ng-app="">
    <p ng-repeat="key in [1,2,3]">
        <input type="text" ng-model="contact.name" />
        {{contact}}
    </p>
</div>

As opposed to the non-dynamic way that works: 与非动态方式相反:

http://jsfiddle.net/Fizk/d0smns1v/ http://jsfiddle.net/Fizk/d0smns1v/

<div ng-app="">
<p>
    <input type="text" ng-model="contact.name" />
    {{contact}}
</p>
    <p>
    <input type="text" ng-model="contact.name" />
    {{contact}}
</p>
<p>
    <input type="text" ng-model="contact.name" />
    {{contact}}
</p>
</div>

The real code is a bit more complicated, and I cannot just hardcode the number of fields, since it's dynamically fetched from an api. 实际的代码要复杂一些,我不能仅仅对字段的数量进行硬编码,因为它是从api动态获取的。

I've looked through tons of questions regarding dynamic model binding and looked through the documentation, but with no luck. 我浏览了有关动态模型绑定的大量问题,并浏览了文档,但是没有运气。

Can anyone shed some light on how I can make all three fields use the same model, so they'll update nicely? 谁能阐明我如何使所有三个字段都使用相同的模型,以便它们能够很好地更新?

Angular 1.3 added a controllerAs option which should solve all your issues when dealing with child scopes and scope inheritance. Angular 1.3添加了controllerAs选项,该选项应在处理子作用域和作用域继承时解决所有问题。

This is considered best practice today. 今天,这被认为是最佳实践。 I've created a plunker for you. 我为您制作了一个小矮人

  <div ng-app="myApp" ng-controller="myCtrl as vm">
      <p ng-repeat="key in [1,2,3]">
          <input type="text" ng-model="vm.contact.name" />
          {{contact}}
      </p>
  </div>

  <script>
    angular.module("myApp", []).controller("myCtrl", function() {
        var vm = this;

        // use vm.value instead of $scope.value
    });

  </script>

I highly recommend reading this article: Understanding Scopes . 我强烈建议您阅读这篇文章: 了解范围

And to understand the new controllerAs syntax you should check out: Exploring Angular 1.3: Binding to Directive Controllers . 并了解新的controllerAs语法,您应该查看以下内容: 探索Angular 1.3:绑定到指令控制器

If you assign contact to be an object in the parent scope before creating the child scope (I'm using ng-init to do this but it would make more sense in a controller), it will work as the child scopes will inherit the reference to the same object. 如果您在创建子范围之前将contact分配为父范围中的对象(我正在使用ng-init进行此操作,但在控制器中更有意义),它将起作用,因为子范围将继承引用到同一个对象。

http://jsfiddle.net/uurL65e5/1/ http://jsfiddle.net/uurL65e5/1/

<div ng-app="" ng-init="contact = {}">
    <p ng-repeat="key in [1,2,3]">
        <input type="text" ng-model="contact.name" />
        {{contact}}
    </p>
</div>

you need to define your model before, usually you define your model in your controller but for instance this works : 您需要先定义模型,通常是在控制器中定义模型,但是例如,这可行:

<div ng-app="">
    <input type="text" ng-model="contact.name" />
    <p ng-repeat="key in [1,2,3]">
        <input type="text" ng-model="contact.name" />
        {{contact}}
    </p>
</div>

Just use $parent 只需使用$parent

<div ng-app="">
    <p ng-repeat="key in [1,2,3]">
        <input type="text" ng-model="$parent.contact.name" />
        {{contact}}
    </p>
</div>

Fiddle - http://jsfiddle.net/ujmd0pc9/ 小提琴-http: //jsfiddle.net/ujmd0pc9/

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

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