在真假之间切换
Toggling between true and false
import Component from '@ember/component';
export default Component.extend({
hello: true ,
actions:{
switch: function(hello){
if (hello === false) {
this.set(hello, true);
} else if (hello === true) {
this.set(hello, false);
}
}
}
});
I'm trying to toggle between the false and true options however this always returns false. I need it so that it switches between the two depending on the its current value. I have a button which runs this function. Initially i want it to change from true to false then if its clicked again to change to true etc...
首先,条件使用===
、!==
、==
等
其次,this.set('hello', !hello)
不需要 If 条件或任何其他条件。
解释:
!
是条件运算符。
它检查条件是否为假、空或未定义。
但是你可以用它来切换真假。
你可以试试这个:
import Component from '@ember/component';
export default Component.extend({
hello: true ,
actions:{
switch: (hello) => {
this.set('hello', (hello ^= 1) == true)
}
}
});
或者这样:
import Component from '@ember/component';
export default Component.extend({
hello: true ,
actions:{
switch: (hello) => this.set('hello', !hello)
}
});
你的逻辑没问题,但错误是你实际上没有设置hello
属性。如所写,您的代码将在控制台中显示错误,从而提供一些线索。更正了下面的代码,注意 'hello'
:
两边的引号
import Component from '@ember/component';
export default Component.extend({
hello: true ,
actions:{
switch: function(hello){
if (hello === false){
this.set('hello', true);
} else if (hello === true){
this.set('hello', false);
}
}
})
在您的原始代码片段中,您操作的 hello
参数的值为 "true"。因此你的代码是说,this.set(true, false)
。 this.set
方法期望接收字符串形式的变量名称及其值。
此外,Ember 组件有一个名为 toggleProperty 的方法,它在这里很有用:
import Component from '@ember/component';
export default Component.extend({
hello: true ,
actions:{
switch() {
this.toggleProperty('hello')
}
}
});
这个问题的其他一些答案(还)没有说明使用 this.set
的需要,这是 Ember 对变量可观察性的特定要求。
此答案适用于 Ember 1.13 至至少 3.x。对于小于 3 的 Ember 版本,import
和 export
行会更改,仅此而已。
我假设您真的不需要开关函数的参数,但总是切换 hello
属性。如果是,则执行此操作:
switch() { // here no parameter
this.set('hello', !this.hello); // dont forget the '' for `this.set`.
}
import Component from '@ember/component';
export default Component.extend({
hello: true ,
actions:{
switch: function(hello){
if (hello === false) {
this.set(hello, true);
} else if (hello === true) {
this.set(hello, false);
}
}
}
});
I'm trying to toggle between the false and true options however this always returns false. I need it so that it switches between the two depending on the its current value. I have a button which runs this function. Initially i want it to change from true to false then if its clicked again to change to true etc...
首先,条件使用===
、!==
、==
等
其次,this.set('hello', !hello)
不需要 If 条件或任何其他条件。
解释:
!
是条件运算符。
它检查条件是否为假、空或未定义。
但是你可以用它来切换真假。
你可以试试这个:
import Component from '@ember/component';
export default Component.extend({
hello: true ,
actions:{
switch: (hello) => {
this.set('hello', (hello ^= 1) == true)
}
}
});
或者这样:
import Component from '@ember/component';
export default Component.extend({
hello: true ,
actions:{
switch: (hello) => this.set('hello', !hello)
}
});
你的逻辑没问题,但错误是你实际上没有设置hello
属性。如所写,您的代码将在控制台中显示错误,从而提供一些线索。更正了下面的代码,注意 'hello'
:
import Component from '@ember/component';
export default Component.extend({
hello: true ,
actions:{
switch: function(hello){
if (hello === false){
this.set('hello', true);
} else if (hello === true){
this.set('hello', false);
}
}
})
在您的原始代码片段中,您操作的 hello
参数的值为 "true"。因此你的代码是说,this.set(true, false)
。 this.set
方法期望接收字符串形式的变量名称及其值。
此外,Ember 组件有一个名为 toggleProperty 的方法,它在这里很有用:
import Component from '@ember/component';
export default Component.extend({
hello: true ,
actions:{
switch() {
this.toggleProperty('hello')
}
}
});
这个问题的其他一些答案(还)没有说明使用 this.set
的需要,这是 Ember 对变量可观察性的特定要求。
此答案适用于 Ember 1.13 至至少 3.x。对于小于 3 的 Ember 版本,import
和 export
行会更改,仅此而已。
我假设您真的不需要开关函数的参数,但总是切换 hello
属性。如果是,则执行此操作:
switch() { // here no parameter
this.set('hello', !this.hello); // dont forget the '' for `this.set`.
}