EmberJS 如何从控制器 属性 在 select 菜单中创建选项

EmberJS How to create an option in a select menu from a controller property

我正在构建一个 ember 应用程序并想创建一个 select 菜单。 select 菜单的选项之一必须是我的数据存储中的产品总数。

我已经为产品总数创建了一个 属性。如何将它放入我创建 select 菜单的数组中?代码如下:

HTML 片段:

{{view "select" content=itemsPerPageOptions}}

控制器代码:

App.ProductsController = Ember.ArrayController.extend({
  totalItems: function(){
    return this.get('length');
  }.property('length'),

  itemsPerPageOptions : [1, 2, 3, 4, 5, this.totalItems],
});

目前,select 菜单中的最后一个选项为空白。

我认为 this.totalItems 在您设置 itemsPerPageOptions

时不可用

试试这个:

App.ProductsController = Ember.ArrayController.extend({
  totalItems: function(){
    return this.get('length');
  }.property('length'),

  itemsPerPageOptions: [],
  init: function() {
    var tmpOptions = [1, 2, 3, 4, 5];
    var newVal = this.totalItems();
    tmpOptions.push(newVal);
    this.set(`itemsPerPageOptions`,tmpOptions);
  }
});

我也不确定 this.get('length') 是否是您要查找的内容,我不希望 return 有任何价值。您可能正在寻找 this.get('model.length')

根据 totalItems 将其设为 属性 并使用 get('totalItems') 因为 totalItems 也是 属性:

itemsPerPageOptions: function(){
    return [1, 2, 3, 4, 5, this.get('totalItems')];
}.property('totalItems')