2016-06-29 8 views
2

Es tut mir leid, wenn dies bereits beantwortet ist, aber das Verhalten von eckig md-select treibt mich zu trinken.Wie setze ich den Initialwert auf md-select innerhalb eines ng-repeats?

Ich habe eine Situation, wo ich ein Array von Objekten erstellen, die einige Felder und ein Auswahlelement enthalten. Ich möchte die Werte im select-Element vorwählen und bekomme seltsames Verhalten.

Hier ist der HTML (mit Winkelmaterial):

<md-list> 
    <md-list-item ng-repeat="condition in conditions track by $index"> 
     <div layout="row" flex> 
     <div flex="5"> 
      &nbsp;&nbsp; 
     </div> 
     <div flex="25"> 
      <md-input-container> 
      <label>From</label> 
      <input required type="number" name="from" minvalue=0 maxvalue="100" ng-model="condition.from"> 
      </md-input-container> 
     </div> 
     <div flex="5"> 
      &nbsp;&nbsp; 
     </div> 
     <div flex="25"> 
      <md-input-container> 
      <label>To</label> 
      <input required type="number" name="to" minvalue=0 maxvalue="100" ng-model="condition.to"> 
      </md-input-container> 
     </div> 
     <div flex="5"> 
      &nbsp;&nbsp; 
     </div> 
     <div flex="25"> 
      <md-input-container> 
      <label>Select Question</label> 
      <md-select ng-model="condition.question[$index]" ng-model-options="{trackBy: '$value._id'}" required name="cQuestion"> 
      <md-option ng-repeat="question in questions | orderBy : '_id'" ng-selected="{{condition.question._id === question._id ? true : false}}" ng-value={{question}}> 
       {{question.title}} 
      </md-option> 
      </md-select>    
      </md-input-container> 
     </div> 
     <div flex="5"> 
      &nbsp;&nbsp; 
     </div> 
     </div> 
    </md-list-item> 
    </md-list> 

Hier ist die js, die die Werte schafft Ich versuche zu testen:

$scope.questions = [ 
    {title: 'Question 1', qtype: 1, _id:1}, 
    {title: 'Question 2', qtype: 2, _id:2}, 
    {title: 'Question 3', qtype: 3, _id:3}, 
    {title: 'Question 4', qtype: 4, _id:4}, 
    {title: 'Question 5', qtype: 5, _id:5} 
]; 
$scope.conditions = []; 
var condition = {}; 
condition.from = ''; 
condition.to =''; 

condition.question = {title: 'Question 2', qtype: 2, _id:3}; 
$scope.conditions.push(condition); 
condition.from = ''; 
condition.to =''; 

condition.question = {title: 'Question 5', qtype: 5, _id:5}; 
$scope.conditions.push(condition); 

Wenn Sie dies ausführen Sie werden feststellen, dass beide Zeilen den Wert Frage 5 ausgewählt haben. Sieht so aus, als ob das Modell verwirrt wird.

Irgendwelche Ideen, was getan werden muss, um dies zu beheben?

Antwort

3

Sie verwenden das alte "condition" -Objekt zweimal. Aber Sie müssen ein neues "Bedingung" -Objekt nach dem Push erstellen.

var condition = {}; 
condition.from = ''; 
condition.to =''; 

condition.question = {title: 'Question 2', qtype: 2, _id:3}; 
$scope.conditions.push(condition); 

condition = {}; // <= this line was added 
condition.from = ''; 
condition.to =''; 

condition.question = {title: 'Question 5', qtype: 5, _id:5}; 
$scope.conditions.push(condition); 
Verwandte Themen