红宝石:设置选择模型的价值不是通过AngularJS结合工作 [英] Ruby: Setting select model value not working via AngularJS binding
问题描述
我有一个情况我设置对象的选择元素外(编辑模式对话框)。当我设置的范围变量值的选择控制只是失去了它的价值,而不是选择相当于项目(如具有相同ID的对象)。
I have a situation where I'm setting the object on a select element externally (an edit modal dialog). When I set the scope variable value the select control just loses it's value instead of selecting the equivalent item (like the object with the same id).
我不知道这是一个不同的NG-选项值我需要的,似乎有填充它的很多方面,我不从文档理解。我使用的基本类型:
I wonder if it's a different ng-option value I need, there seem to many ways of populating it which I don't understand from the documentation. I'm using the basic type:
<select ng-model="color" ng-options="c.name for c in colors"></select>
我创建了一个类似的情况这里。我需要点击按钮设置在选择下拉应有的价值。
I've created a similar situation here. I need clicking the button to set the proper value in the select drop down.
推荐答案
AngularJS进行比较,以供参考,而不是平等。当你作出这样的,AngularJS没有找到 $ scope.colors
任何等效对象,然后设置列表为空值:
AngularJS compares for reference rather than for equality. When you make that, AngularJS doesn't find any equivalent object in $scope.colors
, and then sets the list to an empty value :
$scope.setColor = function() {
$scope.color = {id:12,name:'white',shade:'light'};
};
相反,你可以简单地说,像此琴:
$scope.setColor = function() {
$scope.color = $scope.colors[1];
};
这篇关于红宝石:设置选择模型的价值不是通过AngularJS结合工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!