简体   繁体   English

ng-repeat中的不同ng模型

[英]different ng-models in ng-repeat

I am facing a problem in AngularJS! 我在AngularJS中遇到问题! (I am very new to AngularJS) (我对AngularJS非常陌生)

I am trying to setup n select fields with x options and I want to have different ng-model on the select fields . 我正在尝试使用x options设置n个select fields ,并且我想在select fields上使用不同的ng-model

At the same time I want to disable the option that has been selected in the first select field in all other select fields . 同时,我想禁用在所有其他select fields的第一个选择字段中select fields

I have tried serveral different things. 我已经尝试过服务器上的其他事情。 And the problem was that I could not handle the different models in the ng-repeat 问题是我无法处理ng-repeat中的不同模型

Here is my JSON-File to make things a little bit clearer: 这是我的JSON文件,可以使事情更清晰:

[{
  id: 1,
  text: "Question1",
  selected:false
}, {
  id: 2,
  text: "Question2",
  selected:false
}, {
  id: 3,
  text: "Question3",
  selected:false
}, {
  id: 4,
  text: "Question4",
  selected:false
}, {
  id: 5,
  text: "Question5",
  selected:false
}];

This is one of my attempts. 是我的尝试之一。 The problem is that the more select fields I have the messier it gets. 问题在于更多选择字段使我得到了更多信息。

I have also found this , but I can't get it to work for my questions in example. 我也发现了这个问题 ,但是在示例中我无法解决它。

Basically, what I would need to have is something like this: 基本上,我需要的是这样的东西:

<select ng-change="onChange()" ng-options='q.value for q in questions | filter:{selected:  false}' ng-model='option[$index]'><option value="">-- pick one --</option>  </select>

But for some reason the $index is not interpreted as the current index, but as plain string. 但是由于某种原因,$ index不会解释为当前索引,而是解释为纯字符串。

You could create an array of question sets. 您可以创建一系列问题集。 Using ng-repeat you can create multiple drop downs with the same set of initial options. 使用ng-repeat可以使用相同的初始选项集创建多个下拉菜单。

<div ng-repeat="set in questionSets track by set.id">
      <select ng-model="value" ng-options="question.id as question.text for question in set.questions" ng-change="remove($index+1, value);"></select>
    <br><br>
    </div>

Using ng-change you could pass in the array index of the set of questions. 使用ng-change,您可以传递问题集的数组索引。 Then you could remove the selected question from all of the other question sets. 然后,您可以从所有其他问题集中删除所选问题。

$scope.remove = function(key, val) {
      _.each($scope.questionSets, function(s) {

          if(s.id !== key) { 
              s.questions = _.reject(s.questions, function(q) {
                return q.id=== val;
              });

          }
      })
  }

This doesn't handle the case where a user changes the selection multiple times on the first drop down (and multiple options are thus removed from all others), but you can add on to this general idea. 这不能解决用户在第一个下拉菜单中多次更改选择的情况(因此从所有其他选项中删除了多个选项),但是您可以添加此基本概念。

http://plnkr.co/edit/XOTGEc8PfvbUTYIZcL7G?p=preview http://plnkr.co/edit/XOTGEc8PfvbUTYIZcL7G?p=preview

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

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