简体   繁体   中英

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

I am new to ember. I want to create a dropdown for the countries array using 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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