如何通过angularjs的html页面中的ng-model向对象添加属性? [英] How to add property to object through ng-model in html page in angularjs?

查看:93
本文介绍了如何通过angularjs的html页面中的ng-model向对象添加属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用ng-repeat进行迭代的对象,并且在进行迭代时,我想通过ng-model将每个字段添加到另一个对象中.

I have a object I have iterated using ng-repeat and while iterating I want to add each field to another object through ng-model.

//html 

<div ng-repeat="field in forms.fields track by $index">
    <span ng-if="field.type=='textbox'">
    <span style="width:115px;text-transform: capitalize" class="col-lg-5">{{field.Name}}</span>
    <input type="text" class="form-control" style="width: 172px;padding-left: 20px;display: inline-block;" ng-class="{mystyle:field.Name.$invalid}" id="inp_{{$index}}" ng-model="inputfields.{{field.Name}}">{{field.Name}}<br>
</span></div>

在上面的代码中,我想将field.Name作为属性分配给在js文件中声明为 $ scope.inputfield = {}的输入字段.所以我得到一个错误

In the above code i want to assign field.Name as a property to input field which is declared as a $scope.inputfield={} in js file. So i am getting an error as

 Syntax Error: Token '{' invalid key at column 2 of the expression [{{inputfield.field.Name}}] starting at [{inputfield.field.Name}}].

所以任何人都可以解决这个问题.

So can anyone solve this please.

推荐答案

尝试:

ng-model="inputfields[field.Name]"

这篇关于如何通过angularjs的html页面中的ng-model向对象添加属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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