Javascript Angularjs下拉列表赢得';刷新后不保留值
我已经在这方面工作太久了,试图弄明白它Javascript Angularjs下拉列表赢得';刷新后不保留值,javascript,jquery,angularjs,Javascript,Jquery,Angularjs,我已经在这方面工作太久了,试图弄明白它 <select class="form-control" ng-model="question.sel" ng-change="updateDropDownQuestion(question,question.sel)"> <option ng-repeat="answer in que
<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>
{{answer.name}
没有一个
然后在我的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.updateDropDownQuestion=函数(问题,答案){
重置(问题代码)
$scope.formData[“唯一/选定的问题”]=QUESTION.code
$scope.formData[“唯一/选定答案”]=答案
$scope.formData[question.code+“/”+answer]=true
变量问题=$scope.product.questions
对于(i=0;i尝试使用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正在做什么,你能提供吗?来自官方网站:
注意:ngModel通过引用而不是值进行比较。这在绑定到对象数组时很重要。您可能会发现这有助于设置下拉列表的默认值。请参见下面的示例
angular.module('demoApp',[]).controller('DemoController',function($scope){
$scope.options=[
{label:'one',值:1},
{标签:'two',值:2}
];
//尽管此对象与$scope.options中的对象具有相同的属性,
//Angular认为它们不同,因为它基于引用进行比较
$scope.incorrectlySelected={label:'two',value:2};
//这里我们引用的是同一个对象,因此正确初始化选择框
$scope.correctlySelected=$scope.options[1];
});
不正确
我们可能希望选择框被初始化为“2”,但这不是因为这是两个不同的对象
所选值为{incorrectlySelected.value}。
对的
这里我们引用的是$scope.correctlySelected
中与$scope.options
中相同的对象,因此选择框被正确初始化
所选值为{correctlySelected.value}}。
我已经尝试过这种语法。没有比这更好的了。updateDropDownQuestion确实“重置”了下拉列表。你能提供这方面的代码作为对你的问题的编辑吗?谢谢,我以为我只是在处理简单的字符串。