【问题标题】:set variable value from if condition angular (Ionic 3)从 if 条件角度设置变量值(Ionic 3)
【发布时间】:2017-12-15 07:49:20
【问题描述】:

我正在尝试根据subscribe() 中的条件设置一个值。这是我目前正在做的事情

var statuscode;
      this.http.post('http://192.168.3.223:84/fppb/andro_login',this.loginData)
        .subscribe(
                function(response) { 
                    console.log(response);
                    if(response)
                        statuscode = '1';
                    else
                        statuscode = '2';
                },
                function(error) { 
                        statuscode = '3';
                }
        );
        loader.dismiss();
        console.log(statuscode);

这段代码总是在我的console.log(statuscode); 中给我undefined

除此之外,此页面:http://192.168.3.223:84/fppb/andro_login 正在返回 true 或 false。这是确定响应的代码:

if($query->num_rows() > 0)
        {
            echo json_encode(true);
        }else{
            echo json_encode(false);
        }

我该如何解决这个问题?

对不起,我的英语很差,提前谢谢。

编辑

所以,我想为指定的statuscode 显示alert。这样做时:

  signIn(){
    let loader = this.ld.create({ content: "Please wait..." });
    loader.present();

        var headers = {
                'Access-Control-Allow-Origin' : '*',
                'Access-Control-Allow-Methods' : 'POST'

            };

        let bodyString = JSON.stringify(this.loginData); // Stringify payload

        var statuscode;
          this.http.post('http://192.168.3.223:84/fppb/andro_login',this.loginData)
            .subscribe(
                    function(response) { 
                        console.log(response);
                        if(response)
                        {
                                let alert = this.alertCtrl.create({
                                    title: 'Sukses',
                                    subTitle: 'Login Berhasil',
                                    buttons: ['OK']
                                });
                                loader.dismiss();
                                alert.present();
                        }   
                        else {
                            let alert = this.alertCtrl.create({
                                title: 'Gagal',
                                subTitle: 'Username atau Password salah',
                                buttons: ['OK']
                            });
                            loader.dismiss();
                            alert.present();
                        }
                    },
                    function(error) { 
                            let alert = this.alertCtrl.create({
                            title: 'Gagal',
                            subTitle: 'Username atau Password salah',
                            buttons: ['OK']
                        });
                                loader.dismiss();
                                alert.present();                        
                    }
            );
    }

我收到此错误:

TypeError: 无法读取未定义的“创建”属性

有什么想法吗?

【问题讨论】:

    标签: angular ionic3


    【解决方案1】:

    由于 javascript 是异步的,在 observable 订阅事件之前,控制台日志语句会执行。这就是它打印 undefined 的原因。

    你需要移动subscribe里面的语句

       .subscribe(
                function(response) { 
                    console.log(response);
                    if(response)
                        statuscode = '1';
                    else
                        statuscode = '2';
    
                    console.log(statuscode);
                },
                function(error) { 
                        statuscode = '3';
                }
        );
        loader.dismiss();
    

    【讨论】:

    • 我这样做是因为我需要做一些事情,我想alert.present(); 获取指定的状态码。
    • 为什么不能在订阅里面做呢]
    • 初始化alertCtrl变量
    猜你喜欢
    • 2017-08-04
    • 2018-12-15
    • 2017-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-09
    • 2021-09-14
    • 2013-07-14
    相关资源
    最近更新 更多