2015-10-16 72 views
0

我一直在努力研究這種方法太久了。Angularjs Dropdown在刷新後不會保留價值

<select class="form-control" 
         ng-model="question.sel" 
         ng-change="updateDropDownQuestion(question,question.sel)"> 
         <option ng-repeat="answer in question.answers" ng-disabled="answer.notAnOption" value="{{answer.acode}}">{{answer.name}}</option> 
         <option style="display:none" value="NONE">NONE</option> 
         </select> 

然後在我的js文件:

$scope.updateDropDownQuestion = function(question, answer) { 
    reset(question.code) 

    $scope.formData["SOLE/SELECTED_QUESTION"] = question.code 
    $scope.formData["SOLE/SELECTED_ANSWER"] = answer 

    $scope.formData[question.code+"/"+answer] = true 

    var questions = $scope.product.questions 
    for(i=0; i <questions.length;i++){   
     if(questions[i].code == question.code){ 
      questions[i].sel = answer 
      break; 
     } 
    } 

    $scope.refresh() 
}; 

的$ scope.refresh()是它改變了。這呈現屏幕。

不管我做什麼,它似乎呈現的是以前的狀態,而不是下拉的當前狀態。這是因爲我在下拉更改後重新繪製屏幕。

看起來好像當屏幕重新繪製它是先取得原始值。

任何想法,我怎麼能得到值「堅持」一旦設置? 之後我需要發動一些事件嗎?

回答

0

來自Angular官方網站: 注意:ngModel通過引用比較,而不是數值。綁定到一組對象時這很重要。您可能會發現這有助於設置下拉列表的默認值。看下面的例子。

angular.module('demoApp', []).controller('DemoController', function($scope) { 
 

 
    $scope.options = [ 
 
    { label: 'one', value: 1 }, 
 
    { label: 'two', value: 2 } 
 
    ]; 
 
    
 
    // Although this object has the same properties as the one in $scope.options, 
 
    // Angular considers them different because it compares based on reference 
 
    $scope.incorrectlySelected = { label: 'two', value: 2 }; 
 
    
 
    // Here we are referencing the same object, so Angular inits the select box correctly 
 
    $scope.correctlySelected = $scope.options[1]; 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<body ng-app="demoApp"> 
 
    <div ng-controller="DemoController"> 
 
     <div> 
 
      <h2>Incorrect</h2> 
 
      <p>We might expect the select box to be initialized to "two," but it isn't because these are two different objects.</p> 
 
      <select ng-model="incorrectlySelected" 
 
       ng-options="opt as opt.label for opt in options"> 
 
      </select> 
 

 
      The value selected is {{ incorrectlySelected.value }}. 
 
     </div> 
 
     <div> 
 
      <h2>Correct</h2> 
 
      <p>Here we are referencing the same object in <code>$scope.correctlySelected</code> as in <code>$scope.options</code>, so the select box is initialized correctly.</p> 
 
      <select ng-model="correctlySelected" 
 
       ng-options="opt as opt.label for opt in options"> 
 
      </select> 
 

 
      The value selected is {{ correctlySelected.value }}. 
 
     </div> 
 

 
    </div> 
 
</body>

+0

謝謝,我以爲我只是用簡單的字符串工作。 – jimijon

0

嘗試使用ng-options來呈現選項元素。

東西沿着這些路線:

<select class="form-control" 
        ng-model="question.sel" 
        ng-change="updateDropDownQuestion(question,question.sel)" 
        ng-options="answer.acode as answer.name in question.answers"> 
</select> 

這也要看是什麼updateDropDownQuestion是幹什麼的,你能提供嗎?

+0

我試過語法了。沒有更好的表現。 updateDropDownQuestion確實會「重置」下拉菜單。 – jimijon

+0

你能否提供該代碼作爲你的問題的編輯? – arg20