双向绑定与吴先生重复工作 [英] two way binding not working with ng-repeat

查看:362
本文介绍了双向绑定与吴先生重复工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的NG-重复列表,对此我控制器上分配当前列表项的值另一个属性如下:

I have a simple ng-repeat list, in which i am assigning current list item value to another property on the controller as follows:

  <li ng-repeat="num in list">
    <input type="text" ng-init="value = num" ng-model="value" />
    <button type="button" class="btn btn-primary" ng-click="save()">Save</button>
  </li>

但是当我点击保存按钮,我得到了$ scope.value默认值集。我希望要显示为特定的输入文本的价值。

But when i click the Save button i get default value set for $scope.value. I expect the value for the particular input text to be displayed.

下面是控制器:

 angular.module('myApp', [])
    .controller('MyController', function($scope){
      $scope.value = false;
      $scope.list = [0, 1, 2, 3, 4];

      $scope.save = function() {
         alert($scope.value);
      }
    });

我如何可以访问我的保存功能呼叫控制器一个输入项的更新值。

How can i access the updated value of a input item in my controller on save function call.

下面是相同的plunker: plnkr

Here is the plunker for the same: plnkr

更新:我期待值要提取到控制器没有传递给它作为一个参数

Update: I am expecting the value to be fetched to controller without passing it as a parameter.

推荐答案

哇,我简直不能相信大多数人都错过了这一点。如果您使用的是1.2角,那么你绝对应该由在ngRepeat指令关键字检查轨道。

Wow I can't believe most people have missed this. If you are using Angular 1.2 then you should definitely check out track by keyword in your ngRepeat directive.

<li ng-repeat="num in list track by $index">
    <input type="text" ng-model="list[$index]" />
    <button type="button" class="btn btn-primary" ng-click="save()">Save</button>
</li>

底线的是远离原始类型远绑定时,因为它会给你同步的问题。

Bottom line is stay away from primitive types when binding since it will give you sync issues.

伙计们,请把更多的时间和精力投入到研究您的解决方案,而不是仅仅用于发布点。

Fellas please put more time and effort into researching your solutions instead of just posting for points.

希望这有助于!

这篇关于双向绑定与吴先生重复工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆