绑定模型和控制器的重复(Binding ng-repeat and ng-model in cont

2019-09-28 00:42发布

这是我的第一篇关于这里,所以我承担,我是相当新的AngularJS为好。 我试图建立与NG-重复的格式,并有麻烦缠绕我的头周围的角的东西。

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. 试图让“数据-NG-模式”设置为在NG-重复的每个实例独特的东西

  2. 我怎么从计算()函数访问输入字段的值?

Answer 1:

试图让“数据-NG-模式”设置为在NG-重复的每个实例独特的东西

您可以使用list[key][value] ,将在每个项目不同的ng-repeat

我怎么从计算()函数访问输入字段的值?

一般来说,你可以使用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>

演示小提琴



文章来源: Binding ng-repeat and ng-model in controller