简体   繁体   English

如何使用一组相同的ng-options添加多个select元素

[英]How to add more than one select element with same set of ng-options

This question may be very trivial but its driving me nuts. 这个问题可能很琐碎,但却使我发疯。 I can have n number of elements on my page that are added dynamically. 我可以在页面上动态添加n个元素。 All these will have the same set of options for them. 所有这些都将具有相同的选项集。

<select class="form-control action"
      data-ng-options="action.name for action in statementData" 
      data-ng-model="selectedRules.statementOptions"
> 

This is the mark-up for one of such elements. 这是此类元素之一的标记。

Problem: When I change or select something in one select element, all the others are updated. 问题:当我在一个选择元素中更改或选择某些内容时,所有其他元素都会更新。 I dont want that. 我不想要那个。 This might be happening coz of the same model object. 同一模型对象可能正在发生这种情况。 But if i remove model then there is no data. 但是,如果我删除模型,则没有数据。

Copy that original model to other component. 将该原始模型复制到其他组件。 Use angular.copy() for example. 例如,使用angular.copy()。

If you simply copy the variable, its passed by reference and change to one will change the other. 如果仅复制变量,则该变量将通过引用传递并更改为一个将更改另一个。

Try creating two variables using angular.copy() and use it for two select options. 尝试使用angular.copy()创建两个变量,并将其用于两个选择选项。

Try something like below : 尝试如下所示:

<!-- First select -->
<select class="form-control action"
  data-ng-options="action.name for action in statementData" 
  data-ng-model="selectedRules.statementOptions">

<!-- Second select -->
<select class="form-control action"
  data-ng-options="action.name for action in statementData1" 
  data-ng-model="selectedRules.statementOptions"> 

In Controller you will have to do. 在Controller中,您将必须做。

$scope.statementData1 = angular.copy(statementData);

By using angular.copy() separate copies are made for both variables in $scope. 通过使用angular.copy(),可以为$ scope中的两个变量创建单独的副本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 除了在select元素上,如何使用Angular ng-options? - How to use Angular ng-options other than on a select element? 选择 ng-options 或输出字段中的更多元素,其中包含有关所选 ng-options 的详细信息 - More element in select ng-options or an output field with detalis about ng-options selected <select>使用ng-options查询显示多个字段 - <select> showing more than one field using ng-options query 如何使用ng-options设置从对象读取的选择元素的默认值? - How to use ng-options to set default value of select element reading from object? 如何过滤select元素的ng-options中的数据? - How to filter data in ng-options of a select element? 如何使用ng-options在选择上设置提示? - How do I set a prompt on a select using ng-options? 如何添加过滤器 <select>无<ng-options>在Angular JS中? - How to add filter to <select> without <ng-options> in Angular JS? 如何通过对象数组在SELECT标签中的ng-model中设置对象值。 并且有一种简单的方法可以在ng-options上添加过滤器, - How to set the object value in ng-model, in select tag, from an object array. and is there an easy way to add filters on ng-options, ng-options中的更多选项 - More options in ng-options 如何在 ng-options 中设置默认值 - How to set default value in ng-options
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM