2016-07-26 68 views
1

我是新來的emberjs。 在模板中,我有 {{LabelText的}}如何更新計算屬性?

現在,我想 默認情況下,LabelText的應該是, '你好' 如果條件A,然後LabelText的=哇 如果條件B,然後LabelText的=感謝 有沒有辦法在emberjs中的組件中編寫計算屬性,這將執行以上要求?

回答

0

在您的組件,

labeltext:Ember.computed('conditionVar',function(){ 
var val = this.get('conditionVar'); 
if(Ember.isEqual(val,'conditionA')){ 
    return 'Wow'; 
} 
else if(Ember.isEqual(val,'conditionB')) { 
    return 'Thanks'; 
} 
return 'Hello'; 
}) 

你可能有getter和setter的計算性能,

labeltext: Ember.computed('conditionVar', function() { 
    get(key){ 
     var val = this.get('conditionVar'); 
     if (Ember.isEqual(val, 'conditionA')) { 
     return 'Wow'; 
     }else if (Ember.isEqual(val, 'conditionB')) { 
     return 'Thanks'; 
     } 
     return 'Hello'; 
    }, 
    set(key, value){ 
     // you can set conditionVar so that it will return the same value when you request it again. 
     if(value === 'Wow') { 
     this.set('conditionVar', 'conditionA'); 
     } 
     else if(value === 'Thanks') { 
     this.set('conditionVar', 'conditionB'); 
     } 
    return value; 
    } 
    }), 

參見官方指南:https://guides.emberjs.com/v2.3.0/object-model/computed-properties/