我确信这很简单,但我在JS和Ember的新手中已经足够难倒两天了。初始值来自API作为model.status(int)。 但我的init()代码不会导致select控件显示该项。为什么不?如果有更好的方法,请告诉我。提前谢谢!
这是我的HTML:
{{view Ember.Select id='selectUserStatus'
content=selectUserStatus
optionValuePath="content.val"
optionLabelPath="content.label"
value=status
selectionBinding="selectedUserStatus"
prompt="Select a status"}}
这是JS:
App.UserController = Ember.ObjectController.extend({
init: function() {
var selVal = this.get('model.status');
this.set('selectedUserStatus',[this.get('selectUserStatus')[selVal-1]]);
},
selectUserStatus: [
{label:"Guest", val:"1"},
{label:"Registered",val:"2"},
{label:"Editor",val:"3"},
{label:"Administrator",val:"4"},
],
....
答案 0 :(得分:0)
最好能提供jsFiddle或jsBin。但是,通过查看您的代码,我建议如下:
{{view Ember.Select id='selectUserStatus'
content=userStatusList
optionValuePath="content.val"
optionLabelPath="content.label"
value=status
selectionBinding="selectedUserStatus"
prompt="Select a status"}}
这是JS:
App.UserController = Ember.ObjectController.extend({
init: function() {
var selVal = this.get('model.status');
this.set('selectedUserStatus',[this.get('selectUserStatus')[selVal-1]]);
},
userStatusList: [
{label:"Guest", val:"1"},
{label:"Registered",val:"2"},
{label:"Editor",val:"3"},
{label:"Administrator",val:"4"},
],
}
这是一个jsBin:http://jsbin.com/xawak/3/