如何动态设置模型名称的角度? [英] How can i set model name dynamically in angular?

查看:30
本文介绍了如何动态设置模型名称的角度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在ng-repeat循环中动态设置模型名称

How to set the model name dynamically in ng-repeat loop

    <div ng-controller="parentController">
      <div ng-ccontroller="childController">

         <div data-ng-repeat="category in categories">
           <label >{{category.name}}</label>

           <div class="radio">
             <label><input type="radio" name=? value="1" ng-model=?> value 1</label>
             <label><input type="radio" name=? value="2" ng-model="?"> value 1</label>
          </div>

        </div>

     </div>
   </div>

在上面,我必须动态设置名称和ng-model值,就像发生循环时父控制器的 $ parent.cat0,$ parent.cat1,$ parent.cat2 一样.

Here in above I have to set name and ng-model values dynamically Like $parent.cat0,$parent.cat1,$parent.cat2 of parent controller whenever loop through happens.

我尝试了以下操作:

ng-model = '$parent.cat'+{{$index}}
         or
ng-model = "'$parent.cat1'+{{$index}}"
        or
ng-model = "$parent.cat"+{{$index}}

我尝试了以上方法,但没有解决.还有其他方法可以在angular js中动态设置模型名称.

I tried the above things nothing worked out. Is there any other method to set the model name dynamically in angular js.

我期待这样的东西(以普通html给出)

I am expecting somethong like this (given in normal html)

<label>category1</label>
      <input type="radio" name ="cat1" value=0>value0</input>
      <input type="radio" name ="cat1" value=1>value1</input>
<label>category2</label>
      <input type="radio" name ="cat2" value=0>value0</input>
      <input type="radio" name ="cat2" value=1>value1</input>

推荐答案

在您的情况下,它应该类似于:

in your case it should be something like:

 ng-model = "$parent.cat[$index]"

这篇关于如何动态设置模型名称的角度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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