简体   繁体   English

如何仅更新当前选定的ng-repeat元素而不是全部

[英]How to update only the currently selected ng-repeat element instead of all

I am having the following problem: I have written an app where one can repeatedly add a div that contains two select boxes. 我遇到以下问题:我编写了一个应用程序,可以在其中重复添加一个包含两个选择框的div。 The contents of the second select box depend on the selection of the first select box. 第二选择框的内容取决于第一选择框的选择。 However, once I select a specific item from the first select box, the contents of all existing second select boxes are updated. 但是,一旦我从第一个选择框中选择了一个特定的项目,所有现有的第二个选择框中的内容就会更新。 What I want is only the current second select box to be updated. 我想要的只是当前要更新的第二选择框。

Here is my Plunker 这是我的笨蛋

Whenever I have 1..n select boxes already and then select specialItem4 from the first select box of the current div, all preceding second select boxes are updated as well and contain just the two selection options special1 and special2 which are defined for specialItem4 only. 每当我有1..1已经选择框,然后从当前div的第一个选择框中选择specialItem4,所有前面的第二个选择框,以及更新,只包含两个选择方案special1并且被用于specialItem4仅定义SPECIAL2。 I just want that current second select box to be updated: 我只想更新当前的第二选择框:

A snippet of the HTML code: HTML代码片段:

<div ng-switch-default="ng-switch-default">
      <select ng-model="rule.parameter" ng-options="sel1.name for sel1 in selects1" ng-change="change(rule.parameter)"></select>
      <select ng-options="sel2 for sel2 in filteredselect2" ng-model="rule.relation"></select>
</div>

A snipped of the JS code: 一段JS代码:

    scope.change = function (sel2Selection) {
        scope.filteredselect2 = [];
        for(var key in scope.selects2){
            if(angular.isDefined(scope.selects2[key][sel2Selection.relation])){
                scope.filteredselect2 = scope.selects2[key][sel2Selection.relation];
            }
        }
    };  

I'd be grateful for help as I am a total newbie to AngularJS. 我很感谢您的帮助,因为我是AngularJS的新手。

Since the list to use as the second dropdown varies in each pair, it needs to be isolated. 由于用作第二个下拉列表的列表在每对中各不相同,因此需要将其隔离。 Using a directive for the pair of dropdowns would allow you to encapsulate all the logic related to the two dropdowns together. 对这对下拉列表使用指令可以使您将与这两个下拉列表相关的所有逻辑封装在一起。 In your case move the change listener into the new directive like so: 在您的情况下,将更改侦听器移到新指令中,如下所示:

dynamicSelect.directive('dynamicSelectPair', function() {
  return {
    scope: {
      rule: '=',
      selects1: '=',
      selects2: '='
    },
    template: '<select ng-model="rule.parameter" ng-options="sel1.name for sel1 in selects1" ng-change="change(rule.parameter)"></select><select ng-options="sel2 for sel2 in filteredselect2" ng-model="rule.relation"></select>',
    link: function(scope) {
      // change the second select content depending on the select item of the first select
        scope.change = function(sel2Selection) {
          scope.filteredselect2 = [];

          for (var key in scope.selects2) { 
            if (angular.isDefined(scope.selects2[key][sel2Selection.relation])) {
              scope.filteredselect2 = scope.selects2[key][sel2Selection.relation];
            }
          }
        };
    }
  }  
});

Then change your HTML to use the new directive in your ng-repeat. 然后更改您的HTML以在ng-repeat中使用new指令。 Note that, the possible values for the selects can be passed down into the directive since you already have them in the outer scope. 请注意,因为您已经在外部作用域中将选择的可能值传递给了伪指令。

 <div ng-switch-default="ng-switch-default">
        <div dynamic-select-pair selects1='selects1' selects2='selects2' rule='rule'>
 </div>

Here is an updated Plunkr: http://plnkr.co/edit/Sr1s6I3zleCGr95eYa9e?p=preview 这是更新的Plunkr: http ://plnkr.co/edit/Sr1s6I3zleCGr95eYa9e?p=preview

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM