简体   繁体   English

如何在AngularJs中更新模型时刷新多选框

[英]How to refresh the multiple select box when the model is updated in AngularJs

Please see the following jsfiddle. 请参阅以下jsfiddle。 I put a simple example here to explain what I want to achieve. 我在这里举了一个简单的例子来解释我想要实现的目标。

https://jsfiddle.net/nz9h6aje/1/ https://jsfiddle.net/nz9h6aje/1/

html page: html页面:

<div ng-app="testApp" ng-controller="testCtrl">
1- <md-input-container>
<label>Question</label>
<input ng-model="models.Question1">
</md-input-container>
2- <md-input-container>
<label>Question</label>
<input ng-model="models.Question2">
</md-input-container>
<md-select  placeholder="Dependency" ng-model="models.Denpendency" md-on-open="getQuestionSet()" style="min-width: 300px;" multiple>
<md-option ng-value="setQuestion" ng-repeat="setQuestion in setQuestions">{{setQuestion}}</md-option>
</md-select>
</div>

js file: js文件:

angular.module('testApp', ['ngAnimate',
'ngAria',
'ngMaterial']).controller('testCtrl',function($scope){
$scope.models={
Question1:"",
Question2:"",
Dependency:[]           
}

$scope.getQuestionSet = function() {
  $scope.setQuestions = [];
  var tmpQuestion = 1 + " - " +$scope.models.Question1;
    $scope.setQuestions.push(tmpQuestion);
    tmpQuestion = 2 + " - " +$scope.models.Question2;
    $scope.setQuestions.push(tmpQuestion);
};

 $scope.$watch('models', function (newVal, oldVal) {
  if (newVal.Question1 !== oldVal.Question1) {
        var tmpQuestion = 1 + " - " +$scope.models.Question1;
      $scope.models.Dependency[0] = tmpQuestion;
  }
  if (newVal.Question2 !== oldVal.Question2) {
        var tmpQuestion = 2 + " - " +$scope.models.Question2;
      $scope.models.Dependency[1] = tmpQuestion;
  }
}, true);

});

The value of the multiple selected box will change when the question changes. 当问题发生变化时,多个选定框的值将会更改。 I have a model models.Dependency that stores the selected value of this multiple selected box. 我有一个模型models.Dependency存储这个多选框的选定值。 When the two questions changes, the selected value stored in the models.Dependency will changed as well. 当两个问题发生变化时,存储在models.Dependency中的选定值也会发生变化。 My codes now can change the models.Dependency when the questions changes. 我的代码现在可以更改模型。问题发生变化时的依赖关系。 But the display of the selected box won't change. 但所选框的显示不会改变。 What should I do to refresh the multiple selected box to reflect the new data in models.Dependency. 我该怎么做才能刷新多个选中的框以反映models.Dependency中的新数据。

For example, the two questions are 1- Question 1, 2-Question 2. Then when I click the multiple select box, I will see two options: 1-Question 1, 2-Question 2. When I changes the Questions, the options will change as well. 例如,这两个问题是1-问题1,2-问题2.然后,当我单击多个选择框时,我将看到两个选项:1 - 问题1,2-问题2.当我更改问题时,选项也将改变。 If I selected both of these two Questions, the multiple select box will display as "1-Question 1, 2-Question 2". 如果我选择了这两个问题,则多重选择框将显示为“1-Question 1,2-Question 2”。 If I changes the value of Question 1 to "New Question 1". 如果我将问题1的值更改为“新问题1”。 The display of the multiple select remains the same. 多重选择的显示保持不变。 But if you click it, you will see the options has already been changed. 但是如果单击它,您将看到选项已经更改。 The changed option is not selected. 未选择更改的选项。 This is not correct. 这是不正确的。 I want the changed option is selected and the display of this multiple select box also changes to "1-New Question 1, 2-Question 2". 我希望选择已更改的选项,此多选框的显示也会更改为“1-New Question 1,2-Question 2”。

I hope I explain my requirement clearly. 我希望我能清楚地解释我的要求。 Thank you. 谢谢。

Take a look at: https://jsfiddle.net/u2nvj4fs/2/ 看看: https//jsfiddle.net/u2nvj4fs/2/

A few things: 一些东西:

  1. your ng-model for md-select has a typo (Denpendency instead of Dependency) 你的ng-model for md-select有一个拼写错误(Denpendency而不是Dependency)
  2. You are overwriting the whole set list every time you open the dropdown so it's overwriting the items and removing them from the list. 每次打开下拉列表时,您都会覆盖整个集合列表,因此它会覆盖项目并将其从列表中删除。

This "more or less" does what you're looking for. 这种“或多或少”可以满足您的需求。 It should get you headed in the right direction. 它应该让你朝着正确的方向前进。 I'd be happy to elaborate as needed 我很乐意根据需要详细说明

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

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