简体   繁体   English

如果条件在角度5中不起作用,则为假或空对象

[英]False or empty object if condition not working in angular 5

I'm getting false in the response from the api. 我在api的响应中变得虚假。 If I console.log the result then it shows {} . 如果我console.log结果显示,则显示{} What I want to do is, if the response is false then redirect to the homepage. 我想做的是,如果响应为假,则重定向到主页。 But the false condition is not working. 但是错误的条件不起作用。 Below is my code: 下面是我的代码:

this.sub = this.route.params.subscribe(params => {
    this.token = params['Token'];
    this.common.createAPIService('api/users/recoverpassword/ValidateLink?Token=' + this.token + '', '').subscribe((result: any) => {

        console.log(result);
        if (!result || result == {} || result == false) {
            this.common.ShowNotification("Change Password", "Invalid Request", "error");
            // setTimeout(function () {
            //     window.location.href = "#";
            // }, 3000);
            console.log(12);
            this.zone.run(() => this.router.navigateByUrl("/home/" + "Mumbai"));
        }
    });
});

I tried !result , {} , even result == false but it's not working. 我尝试了!result{} ,甚至result == false但它不起作用。 The alert in condition or console.log not coming. 条件或console.log的警报未到。 Please help. 请帮忙。

Use === instead of == 使用===代替==

 if (!result || result === {} || result === false) {
                    this.common.ShowNotification("Change Password", "Invalid Request", "error");
                    // setTimeout(function () {
                    //     window.location.href = "#";
                    // }, 3000);
                    console.log(12);
                    this.zone.run(() => this.router.navigateByUrl("/home/" + "Mumbai"));
                }

For more you can refer this == vs === 有关更多信息,请参考== vs ===

In Javascript, you should check whether it is truthy or falsy in your condition 在Javascript中,您应检查自己的状况是真实还是虚假

Refer https://developer.mozilla.org/en-US/docs/Glossary/Truthy 请参阅https://developer.mozilla.org/en-US/docs/Glossary/Truthy

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM