动态的ng-model变成ng-repeat AngularJS

Dynamic ng-model into ng-repeat AngularJS

本文关键字:ng-repeat AngularJS 变成 ng-model 动态      更新时间:2023-09-26

我试图在ng-repeat中动态生成ng-model指令,但我从浏览器收到错误。我们得到一个类型的动态属性,我想在DOM中设置它。

我收到这个错误:

Error: [$parse:syntax] Syntax Error: Token '{' invalid key at column 2 of the expression [{{'attribute.'+attribute.label}}] starting at [{'attribute.'+attribute.label}}].

点击这里

<div class="form-group" ng-repeat="attribute in objectType.objectAttributes | orderBy : attribute.order">
     <div class="col-sm-10" ng-if="attribute.multivalued==false">
          <input type="{{attribute.type}}" class="form-control"
              ng-model="{{'attribute.'+attribute.label}}">
     </div>
</div>
你有什么办法可以帮我解决这个问题吗?谢谢你!

这不是正确的形式,如果我尝试了,会出现错误,但最后我这样做了,然后我得到了整个属性列表。如果更改输入,则更改属性。值转换为每个属性。这是非常棘手的,但工作!谢谢你!

<div ng-repeat="attribute in indoor.values | orderBy : attributes[$index].order">
     <input type="{{attributes[$index].type}}" class="form-control"
          ng-model="attribute.value">
</div>

你可以这样做:

ng-model="attribute.{{attribute.label}}"

不要在ng-model中使用{{}}。动态ng模型使用[]。在你的情况下,像这样使用

<div class="form-group" ng-repeat="attribute in objectType.objectAttributes | orderBy : attribute.order">
 <div class="col-sm-10" ng-if="attribute.multivalued==false">
      <input type="{{attribute.type}}" class="form-control"
          ng-model="value1[attribute.label]">
 </div>

其中ng-model中的"value1"是您的输入值。但是请记住使用$scope。值=控制器中的[]。我希望它能帮助你