如何从控制器触发动态ng模型?

时间:2016-10-15 08:37:15

标签: javascript angularjs angular-ngmodel ng-controller

我在我的html中运行一个循环,每次迭代都会创建动态模型。

<tr ng-repeat="item in voucherItems">
       <td><input type="text" ng-model="this['id-' + $index]" typeahead="brand.id as brand.BRANDNAME for brand in brandList" class="input-group" /></td>                     

</tr>

模型将像id1,id2,id3等。 现在我想在我的角度控制器中使用该动态模型。

function GetValue(indexno) {
      var model=scope."id"+indexno;
      dosomething(model);
 }

var model的值将是在html中创建的scope.id1或scope.id2或scope.id3的值。现在问题是

  

范围。 “ID” + indexno

不能像scope.id1或scope.id2或scope.id3那样工作,它将是动态模型而不是它显示错误。问题是如何在角度控制器中编写这个引用的部分像scope.id1或scope.id2一样工作或如何动态触发该模型?

1 个答案:

答案 0 :(得分:0)

我得到了一个解决方案。在html中

  

NG-模型= ID [$指数]

控制器中的

。我必须先声明

  

scope.id = {};

然后在GetValue方法

function GetValue(indexno) {
      var model=scope.id[indexno];
      dosomething(model);
 }

对我有用。 scope.id [indexno] 像scope.id1或scope.id2等工作。感谢@Nora和@gianlucatursi。

观察您可以声明scope.id = []而不是scope.id = {}它也可以。

相关问题