2 回答

TA貢獻1851條經驗 獲得超3個贊
if (!this.myValue || this.myValue==null) 這也將檢查 undefined 和 null。
myImage() {
console.log('test')
this.myValue = this.myObservables.status
console.log(typeof(this.myValue))
if (!this.myValue || this.myValue==null)
{
console.log('undefined')
this.ahuValue = 0
}
else{
// do something
}

TA貢獻2051條經驗 獲得超10個贊
您可以通過添加來檢查是否this.myValue有一個假值if(!this.myValue),那么只有當 this.myValue 的值為 0、空字符串、未定義或 null 時,才會執行 if 語句。
myImage() {
console.log('test')
console.log(typeof(this.ahuObservables.on_off_status))
this.myValue = this.myObservables.status
console.log(typeof(this.myValue))
if (!this.myValue)
{
// this part of the code will be executed if this.myValue is 0, empty string, undefined or null
console.log('undefined')
this.ahuValue = 0
}
else{
// do something
}
添加回答
舉報