Ember.js 如何更新计算属性?
我是新来的。 在模板中,我有 {{labeltext}} 现在,我想要 默认情况下,labeltext应该是“Hello” 如果条件A,则labeltext=Wow 如果条件B,则labeltext=谢谢 有没有办法在emberjs中的组件中写入将执行上述要求的计算属性?在您的组件中Ember.js 如何更新计算属性?,ember.js,Ember.js,我是新来的。 在模板中,我有 {{labeltext}} 现在,我想要 默认情况下,labeltext应该是“Hello” 如果条件A,则labeltext=Wow 如果条件B,则labeltext=谢谢 有没有办法在emberjs中的组件中写入将执行上述要求的计算属性?在您的组件中 labeltext:Ember.computed('conditionVar',function(){ var val = this.get('conditionVar'); if(Ember.isEqual(
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;
}
}),
请参阅组件中的官方指南:
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;
}
}),
参考官方指南: