AngularJS:NG-重复使用键值 - 更新对象 [英] AngularJS: ng-repeat with key value - updating object

查看:172
本文介绍了AngularJS:NG-重复使用键值 - 更新对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我渲染键:值对象数组,像这样NG-重复:

 < D​​IV NG控制器=mainCtrl>
  < D​​IV NG重复=记录中记录>
    < D​​IV NG重复=(键,值)记录>
        <输入NG模型=键/>:<输入NG模型=值/>
    < / DIV>
  < / DIV>
< / DIV>

JS:

  VAR mainCtrl =功能($范围){
$ scope.records = [
        {'键1':'VAL1},
        {'KEY2':'val2的'}
        ];
}

问题是,键和值无法通过输入标签进行更新。出于某种原因,它成为制造NG重复迭代(键,值)后一种方式的结合。

小提琴: http://jsfiddle.net/BSbqU/1/

我怎样才能使一个双向绑定?或者我应该以不同的方式解决这个问题,那么嵌套的NG-重复?


解决方案

 < D​​IV NG控制器=mainCtrl>
< D​​IV NG重复=记录中记录>        <输入NG模型=record.name/>:<输入NG模型=record.value/>
    < / DIV>
< / DIV>

和JS的:

  VAR对myApp = angular.module('对myApp',[]);VAR mainCtrl =功能($范围){
$ scope.records = [
{'名':'键1','值':'VAL1},
{'名':'键2','值':'val2的'}
        ];
}

I am rendering key:value object array with ng-repeat like this:

<div ng-controller="mainCtrl">    
  <div ng-repeat="record in records">
    <div ng-repeat="(key, value) in record">
        <input ng-model="key" />: <input ng-model="value" />
    </div>
  </div>
</div>

JS:

var mainCtrl = function($scope){
$scope.records = [
        {'key1':'val1'},
        {'key2':'val2'}
        ];
}

Problem is that keys and values cannot be updated through input tags. For some reason it becomes one way binding after making ng-repeat iterate over (key,value).

Fiddle: http://jsfiddle.net/BSbqU/1/

How can I make it a two way binding? Or should I approach this problem in a different way then nested ng-repeat?

解决方案

<div ng-controller="mainCtrl">    
<div ng-repeat="record in records">

        <input ng-model="record.name" />: <input ng-model="record.value" />
    </div>
</div>

And the JS:

var myApp = angular.module('myApp', []);

var mainCtrl = function($scope){
$scope.records = [
{'name':'key1','value':'val1'},
{'name':'key2', 'value':'val2'}
        ];
}

这篇关于AngularJS:NG-重复使用键值 - 更新对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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