[英]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.