在Handlebars模板中迭代对象

时间:2014-08-25 17:58:40

标签: loops object ember.js each handlebars.js

如何在Ember应用程序中的手柄模板中迭代对象?

<div class="alert alert-danger">
  <strong>We couldn't create this Company.</strong>
  {{#each errors}          
    <p>{{this}}</p>
  {{/each}}
</div>

console.log(response.errors);

Object {0: "Name can't be blank", 1: "Zip code can't be blank", 2: "Company type can't be blank", 3: "Address line can't be blank", 4: "Country can't be blank", 5: "State can't be blank"}

我在控制台中遇到的错误是:

  

未捕获错误:断言失败:#each循环的值必须   是一个数组。你通过{0:名字不能为空,1:邮政编码不能   空白,2:公司类型不能为空,3:地址行不能为空,   4:国家不能为空,5:国家不能空白}

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

正如您的控制台错误所述,each帮助程序仅适用于数组,因此它不会直接使用您的errors对象。但是,您可以为errors对象创建一个计算属性,并使用jQuery.map(或类似的映射函数)将对象创建到数组中。

errorsArray: function() {
  return $.map(errors, function( value, key ) {
    return value;
  });
}.property('errors')
相关问题