在控制器绑定NG重复和NG-模型 [英] Binding ng-repeat and ng-model in controller

查看:83
本文介绍了在控制器绑定NG重复和NG-模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我与我在这里这么熊的第一篇文章,我是相当新的AngularJS为好。我试图建立与NG重复的格式,并有麻烦缠绕我的头周围的角的东西。

JS在控制器:

  $ scope.myCurrentAssets = {
        cash_equiv:{名称:'现购自等价物',值:0,TBI:41},
        投资:{名称:'短期投资',值:0,TBI:42},
        notes_rec:{名称:应收票据,值:0,TBI:43},
        account_rec:{名称:'应收账款',值:0,TBI:44},
        盘点:{名称:'库存',值:0,TBI:45},
        prepaid:{名称:'prepaid费',值:0,TBI:46},
        其他:{名称:其他流动资产,值:0,TBI:47}
    };

HTML

 < D​​IV CLASS =行中myCurrentAssets(keyAssets,valueAssets)&GT纳克重复=;
     <跨度> {{valueAssets.name}}< / SPAN>
     <输入数据-NAME =myCurrentAssets {{keyAssets}}。
          数据-NG-模式=
          数据占位符={{valueAssets.value}}
          数据-NG-变化=计算()
          />
< / DIV>

时遇到的问题是:


  1. 试图获得数据-NG-模式设置为在NG-重复的每个实例独一无二的东西


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



解决方案

  

试图获得数据-NG-模式设置为在NG-重复的每个实例独一无二的东西。


您可以使用列表[关键] [值] ,将每个项目的不同之处 NG-重复


  

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


一般来说,你可以使用 NG-模型自动获取输入数据。

 < D​​IV CLASS =行中myCurrentAssets(keyAssets,valueAssets)&GT纳克重复=;
     <跨度> {{valueAssets.name}}< / SPAN>
     <输入数据-NAME =myCurrentAssets [keyAssets] ['名']
          数据-NG-模式=myCurrentAssets [keyAssets] ['值']
          数据占位符=myCurrentAssets [keyAssets] ['值']
          数据-NG-变化=计算(myCurrentAssets [keyAssets] ['值'])
          />
< / DIV>

演示<大骨节病> 小提琴

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?

解决方案

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

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

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

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 Fiddle

这篇关于在控制器绑定NG重复和NG-模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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