简体   繁体   English

如何在 ember 2.0 中使用 {{#each}} 助手?

[英]How to use {{#each}} helper in ember 2.0?

I am new to ember.我是 ember 的新手。 I want to create a dropdown for the countries array using ember.我想使用 ember 为国家/地区数组创建一个下拉列表。 I have tried the following:我尝试了以下方法:

App.SignupRoute = Ember.Route.extend({
model: function () {
    countries: { };
    var self = this;
    $.ajax({
        url: "View/countries.json",
        type: "GET",
        async: false,
        success: function (data) {
            self.set("countries",JSON.parse(data));
        }
    });
 }
});

  <select>
     {{#each model as |countries|}}
          <option value={{countries.code}}>{{countries.name}}</option>           
     {{/each}}
  </select>

Could you help me solve this?你能帮我解决这个问题吗?

You are not returning anything from the model hook.您没有从模型挂钩返回任何内容。

Try using this approach instead.尝试改用这种方法。

model: function () {

    var country_promise  = $.ajax({
         url: "View/countries.json",
         type: "GET"
    });

    return Ember.RSVP.hash({
       countries: country_promise
    })
});

hbs乙肝

      <select>
         {{#each model.countries as |country|}}
              <option value={{country.code}}>{{country.name}}</option>           
         {{/each}}
      </select>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM