gpt4 book ai didi

javascript - 按钮上的禁用属性

转载 作者:行者123 更新时间:2023-12-03 02:05:24 25 4
gpt4 key购买 nike

我有一个像这样的简单的 Angular 代码:

import { Component } from '@angular/core';

@Component({
selector: 'app-root',
template: `
<input type="number" #mytext (keyup)="onclickfunc(mytext.value)">
<button [disabled]="myVar==0" > Test </button>`,
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'app';
myVar=0;

onclickfunc(mytext){
this.myVar=mytext;
}
}

正如您所看到的,当 myVar 为 0 时,按钮应该被禁用。代码按原样工作正常。但是,如果我将 [disabled]="myVar==0" 更改为 [disabled]="myVar===0",它的行为就会很奇怪。我的意思是,即使我在输入框中输入 0,该按钮也不会被禁用。

能解释一下原因吗?

最佳答案

=== 运算符检查值和类型,而 == 运算符仅检查值。例如:

1 == "1" -> true
1 === "1" -> false (because types are not equal)

关于javascript - 按钮上的禁用属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49835985/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com