0

尝试使用来自 json 对象数组的选项动态创建选择输入。JSON 对象数组如下所示:

var curr_sel = [
    {v:"1", n:"USD"},
    {v:"2", n:"GBP"},
    {v:"3", n:"CAD"},
    {v:"4", n:"AUD"},
    {v:"5", n:"EUR"}
];

我的视图模板具有以下内容:

<select>
{{#each opt in curr_sel}}
    <option value="{{ opt.v }}" >{{ opt.n }}</option>
{{/each}}
</select>

但这会产生一个没有选项的选择元素。请帮忙。如果这意味着使用 EmberJS 选择类http://emberjs.com/api/classes/Ember.Select.html会更好

4

1 回答 1

1

select使用标签的更好方法是使用Ember.Select.

这是一个示例:

模板

<script type="text/x-handlebars" data-template-name="application">   
  {{view Ember.Select content=curr_sel
    optionLabelPath="content.n"
    optionValuePath="content.v"
    selection=selectedCurrency}}
</script>

Javascript

App = Ember.Application.create({});

App.ApplicationController = Ember.Controller.extend({
    curr_sel: [
        {v:"1", n:"USD"},
        {v:"2", n:"GBP"},
        {v:"3", n:"CAD"},
        {v:"4", n:"AUD"},
        {v:"5", n:"EUR"}
    ],
    selectedCurrency: null,
    selectedCurrencyChanged: function() {
        console.log(this.get('selectedCurrency.n'));
    }.observes('selectedCurrency')
});

看看这个小提琴看看这个在行动http://jsfiddle.net/marciojunior/FJkEr/

于 2013-10-21T16:09:31.703 回答