在控制器中绑定ng-repeat和ng-model

时间:2013-12-20 23:07:43

标签: angularjs ng-repeat

这是我在这里的第一篇文章,所以请耐心等待,我对AngularJS也很新。我正在尝试使用ng-repeat构建一个表单,并且无法将我的头部缠绕在有角度的东西上。

控制器中的JS:

$scope.myCurrentAssets = {
        cash_equiv: {name: 'Cash & Equivalents', value: 0, tbi: 41},
        invest: {name: 'Short Term Investments', value: 0, tbi: 42},
        notes_rec: {name: 'Notes Receivable', value: 0, tbi: 43},
        account_rec: {name: 'Accounts Receivable', value: 0, tbi: 44},
        inventory: {name: 'Inventory', value: 0, tbi: 45},
        prepaid: {name: 'Prepaid Expenses', value: 0, tbi: 46},
        other: {name: 'Other Current Assets', value: 0, tbi: 47}
    };

HTML:

<div class="row" ng-repeat="(keyAssets, valueAssets) in myCurrentAssets">
     <span>{{valueAssets.name}}</span>
     <input data-name="myCurrentAssets.{{keyAssets}}"
          data-ng-model=""
          data-placeholder="{{valueAssets.value}}"
          data-ng-change="compute()"
          />
</div>

我遇到的问题是:

  1. 尝试将'data-ng-model'设置为每个ng-repeat实例中的唯一内容

  2. 如何从compute()函数访问输入字段的值?

1 个答案:

答案 0 :(得分:1)

  

尝试将'data-ng-model'设置为每个ng-repeat实例中的唯一内容

您可以使用list[key][value]

中每个项目不同的ng-repeat
  

如何从compute()函数中访问输入字段的值?

通常,您可以使用自动提取ng-model数据的input

<div class="row" ng-repeat="(keyAssets, valueAssets) in myCurrentAssets">
     <span>{{valueAssets.name}}</span>
     <input data-name="myCurrentAssets[keyAssets]['name']"
          data-ng-model="myCurrentAssets[keyAssets]['value']"
          data-placeholder="myCurrentAssets[keyAssets]['value']"
          data-ng-change="compute(myCurrentAssets[keyAssets]['value'])"     
          /> 
</div>

演示 Fiddle