本文介绍了在控制器中绑定 ng-repeat 和 ng-model的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

控制器中的JS:

$scope.myCurrentAssets = {cash_equiv: {name: 'Cash &等价物',值:0,tbi:41},投资:{名称:'短期投资',价值:0,tbi:42},notes_rec: {name: 'Notes Receivable', value: 0, tbi: 43},account_rec: {name: 'Accounts Receivable', value: 0, tbi: 44},库存:{名称:'库存',值:0,tbi:45},prepaid: {name: 'Prepaid Expenses', value: 0, tbi: 46},其他:{名称:'其他流动资产',价值:0,tbi:47}};

HTML:

<span>{{valueAssets.name}}</span><input data-name="myCurrentAssets.{{keyAssets}}"数据-ng-model=""数据占位符="{{valueAssets.value}}"数据-ng-change="计算()"/>

我遇到的问题是:

  1. 试图将 'data-ng-model' 设置为每个 ng-repeat 实例中唯一的东西

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

解决方案

您可以在 ng-repeat

中使用每个项目不同的 list[key][value]

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

通常您可以使用 ng-model 自动获取 input 数据.

<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'])"/>

演示

This is my first post on here so bear with me, I'm fairly new to AngularJS as well. I'm trying to build a form with ng-repeat and having trouble wrapping my head around the angular stuff.

JS in controller:

$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>

The problems I'm having are:

  1. trying to get the 'data-ng-model' set to something unique in each instance of the ng-repeat

  2. how do I access the value of the input field from the compute() function?

解决方案

You can use list[key][value] that will be different per item in ng-repeat

Generally you can use ng-model that automatically fetches input data.

<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>

Demo

这篇关于在控制器中绑定 ng-repeat 和 ng-model的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 18:54
查看更多