Angular - ng-repeater 中的动态 ng-model [英] Angular - Dynamic ng-model inside ng-repeater

查看:24
本文介绍了Angular - ng-repeater 中的动态 ng-model的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于搜索数据的表单,一些字段是在 ng-repeat 中动态生成的.问题是将 ng-model 分配给我必须进行搜索的所有字段.

我需要使用 {{masterAttribute.Name}} 作为 ng-model,或者我可以在搜索中读取的任何内容.

HTML 是

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script><body ng-app="MyApp" ng-controller="ricercaAttivita"><div class="accordion-group" ng-repeat="masterAttributes 中的masterAttribute"><select class="trip dark" ng-model="{{masterAttribute.Name}}" ng-options="attr.Id as attr.Value for attr in masterAttribute.Values"><option value="">TUTTE</option></选择>

<button class="btn btn-default btn-green btn-3-column sidebar-filtri-button" ng-click="search()">Cerca</button>

解决方案

ng-model="{{masterAttribute.Name}}" 更改为 ng-model="masterAttribute.Name".

因为它已经在 Angular 范围内,所以不需要插值运算符({{..}}).

进一步:search() 方法是你的 ricercaAttivita 控制器的一部分,那么你也可以在控制器中访问 masterAttributes ,你可以在那里找到所有 Name 道具.

I have a form for search data, some fields are generated dinamically inside a ng-repeat. The problem is to assign ng-model to all fields that i must get to do the search.

I need to use the {{masterAttribute.Name}} as ng-model, or whatever that i can read to do in my search.

HTML is

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<body ng-app="MyApp" ng-controller="ricercaAttivita">
    <div class="accordion-group" ng-repeat="masterAttribute in masterAttributes">
        <select class="trip dark" ng-model="{{masterAttribute.Name}}" ng-options="attr.Id as attr.Value for attr in masterAttribute.Values">
            <option value="">TUTTE</option>
        </select>
    </div>
    <button class="btn btn-default btn-green btn-3-column sidebar-filtri-button" ng-click="search()">Cerca</button>
</body> 

解决方案

Change ng-model="{{masterAttribute.Name}}" to ng-model="masterAttribute.Name".

since it's already in Angular scope, so no need of interpolation operator({{..}}).

Further : search() method is part of your ricercaAttivita controller, then you also have access to masterAttributes as well in controller., you can find all Name props there.

这篇关于Angular - ng-repeater 中的动态 ng-model的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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