【发布时间】:2018-12-26 18:21:23
【问题描述】:
我开发了一个简单的打字稿来比较从下拉值传递的值。
请在下面查看我的代码
即使 value = 'Supervisor',选择总是转到 else 方法。
updateFilter(value: string): void {
// value = 'Supervisor'
if (value === 'Supervisor') {
this.loading = true;
this._dashboardservice.getAllSupervisor().subscribe(
resp => {
this.loading = false;
this.listofmodules = Module [resp.length];
this.listofmodules = resp;
}
);
} else {
this._nzMessage.create('error', 'Only Supervisor are allowed at the moment');
}}
但如果我将 === 更改为 == 。一切正常,但是IDE提示我错误== should be === tslint(triple-equals)
感谢任何建议。谢谢stackoverflow。
更新 尝试更多解决方案。检查它是否相同类型。但仍然没有运气。请参阅下面的更新代码
updateFilter(value: string): void {
console.log('Log value: ' + value);
let tempvalue: string;
tempvalue = 'Supervisor';
if (value === tempvalue) {
this.loading = true;
this._dashboardservice.getAllSupervisor().subscribe(
resp => {
this.loading = false;
this.listofmodules = Module [resp.length];
this.listofmodules = resp;
}
);
} else {
this._nzMessage.create('error', 'Only Supervisor are allowed at the moment');
}}
即使console.log('Log value: ' + value); 显示Log value: Supervisor。
第二次更新
我尝试打印 value 和 tempvalue 的 typeof。
value = object
tempvalue = string
为什么会这样?在我的功能开始时,我已经声明value: string
有什么想法吗?
【问题讨论】:
-
只是为了确认,两者是同一类型?
-
如果您手动提供
value = 'Supervisor'而不是从下拉菜单中,那么它也会转到其他部分? -
@ameruddinjamil
===表示完全相同的值,==表示类似于检查您的值类型并检查大小写 -
@ashishpal 如果我手动给出值,它将转到 if 部分。你能告诉我为什么吗?
-
@Abhishek 那么,我应该增强代码的哪一部分?
标签: angular typescript