![](/img/trans.png)
[英]ember-power-select Custom Search Action and 'selected' with external data
[英]How to pass a function in selected from ember-power-select
我有一個Ember.js應用程序,對於其中的表單,我正在使用ember-power-select 。 我已經使用了該插件,但是在配置selected
元素方面存在一些問題。 如標題所示,我正在尋求有關如何使用功能配置selected
幫助。
我想使用從服務檢索變量的函數來設置選定的變量。
我的組件看起來像這樣:
import Ember from 'ember';
export default Ember.Component.
appInfo: Ember.inject.service('app-info'),
languageOption: ['Français', 'English'],
language() {
/*
* In my service appInfo, I have a function called getLocale
* that return the current locale variable.
*/
return this.get('appInfo').getLocale();
},
actions: {
changeLanguage(lang)
{
this.set('language', lang);
}
}
});
我的模板如下所示:
{{#power-select
selected=language
options=languageOption
searchEnabled=false
onchange=(action 'changeLanguage')
placeholder=language
as |lang|}}
{{lang}}
{{/power-select}}
如您所見,我正在嘗試使用通過服務appInfo
獲得的當前locale
設置selected
值。 除了選定的部分,其他一切都正常。
在模板中 ,您可以看到'selected'是可以在組件內設置的值,除了我不想使用硬編碼的值設置,而是使用locale的當前值設置值。
每個提示和/或幫助,不勝感激!
剛解決它,我使用了init()
使其工作。 就像@BrandonW在評論中說的那樣,計算屬性也不起作用。
這是我放入組件中的代碼:
***
init() {
this._super(...arguments);
let loc = this.get('appInfo').getLocale();
if (loc === 'fr')
this.set('language', 'Français');
else
this.set('language', 'English');
}
***
如果要使用在ember-power-select插件中設置selected
功能的方法 ,這就是您要執行的操作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.