簡體   English   中英

Ember JS選擇默認值不起作用

[英]Ember js select default value not working

我想用另一個模型的類別自動填充下拉列表。 它正在顯示,但是默認值不起作用。 這是兩個模型:

App.Task = DS.Model.extend({
  category_id : DS.attr('number'),
  title       : DS.attr('string'),
  ...
  category    : DS.belongsTo('category', { async : true })
});

App.Category = DS.Model.extend({
  name        : DS.attr('string'),
  ...
  tasks       : DS.hasMany('task', { async : true })
});

這是我的路線:

App.Router.map(function () {
   this.resource('tasks', { path : '/tasks'}, function () {
       this.resource('task', { path: '/:task_id' }, function( ) {
           this.route('edit');
       });
       ...
    });
    ...
});

這是控制器:

App.TaskController = Ember.ObjectController.extend({
  isEditing: false,
  needs: ['categories'],
  categoriesDropdown : function() {
     return this.store.find('category');
  }.property(),
  ...
});

我有一個按鈕可以在編輯模式之間切換。 在編輯模式下,將填充下拉列表,但默認值不是。 我使用Ember檢查器,值綁定正在工作,因為如果我選擇一個選項,category_id也會更改。

{{view Ember.Select contentBinding="categoriesDropdown" 
optionValuePath="content.id" optionLabelPath="content.name" 
valueBinding="category_id" class="form-control select-chosen" 
prompt="No category selected"}}

我還嘗試將其直接綁定到類別,但是在加載時類別設置為null,如果我嘗試更改選項,則類別會更改,但是select選項不會更改。

{{view Ember.Select contentBinding="categoriesDropdown" 
optionValuePath="content.id" optionLabelPath="content.name" 
selectionBinding="category" class="form-control select-chosen" 
prompt="No category selected"}}

綁定正在工作,但選擇/下拉列表未更新。

像這樣嘗試:

{{view Ember.Select content=categoriesDropdown
                optionLabelPath="content.name"
                optionValuePath="content.id"
                classNames=["form-control","select-chosen"]
                prompt="No category selected"
                selection=selectedCategory}}

然后,您應該能夠選擇當前選擇作為控制器的selectedCategory屬性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM