代码之家  ›  专栏  ›  技术社区  ›  YVS1102

显示来自其他功能的警报

  •  -2
  • YVS1102  · 技术社区  · 8 年前

    我正在尝试在我的ionic应用程序中显示来自其他功能的警报。我就是这么做的

    console.log(response); //return true
    if (response) {
      this.successAlert;
    } else {
      this.failedAlert;
    }
    

    这是我的警告

    successAlert() {
    this.alertCtrl.create({
        title: "Success"
      }).present();
    }
    
    failedAlert() {
    this.alertCtrl.create({
        title: "Failed"  }).present();
    }
    

    但当我单击它时,没有显示警报,我错过了什么?

    更新-- 以下是的完整代码 home.ts

    import { Component, ViewChild } from '@angular/core';
    import { NavController, App ,LoadingController,AlertController } from 'ionic-angular';
    import { BrowserModule } from '@angular/platform-browser';
    import { HttpClient, HttpHeaders,HttpClientModule } from '@angular/common/http';
    import { Events } from 'ionic-angular';
    
    
    @Component({
      selector: 'page-home',
      templateUrl: 'home.html'
    })
    export class HomePage {
        loginData = {};
    
      constructor(public navCtrl: NavController, 
                    public app: App,
                    public ld: LoadingController,
                    public http:HttpClient,
                    public alertCtrl: AlertController,
                    public events: Events
                    ) {
    
    
      }
    
      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); 
              this.http.post('http://192.168.3.223:84/fppb/andro_login',this.loginData)
                .subscribe(
                        function(response) { 
                            console.log(response);
                            if(response)
                            {
                                this.successAlert();
                            }   
                            else {
                                this.failedAlert();
                            }
                        },
                        function(error) { 
                            this.failedAlert();
                        }
                );
        }
    
        successAlert() {
        this.alertCtrl.create({
            title: "Success",
             subTitle: '10% of battery remaining',
              buttons: ['Dismiss']
          }).present();
        }
    
        failedAlert() {
        this.alertCtrl.create({
            title: "Failed"  }).present();
        }
    
      }
    

    我已经试着改变了

    this.successAlert; this.successAlert();

    但我得到了这个错误

    TypeError:此。successAlert不是一个函数

    1 回复  |  直到 8 年前
        1
  •  2
  •   sebaferreras    8 年前

    您需要使用paranthesis调用函数

    if (response) {
      this.successAlert();
    } else {
      this.failedAlert();
    }
    

    编辑

    我已经试着改变了 this.successAlert; to this.successAlert(); 但我有一个错误: TypeError:此。successAlert不是一个函数

    就像 @Suraj Rao 在评论中提到,您需要使用 箭头功能 ,如下所示:

    // ...          
    this.http.post('http://192.168.3.223:84/fppb/andro_login',this.loginData)
        .subscribe(
           (response) => { // <---- Here!
                console.log(response);
                if(response) {
                    this.successAlert();
                }   
                else {
                    this.failedAlert();
                }
            },
            (error) => { // <---- And here as well
                 this.failedAlert();
            }
         );
    
    // ...
    
    推荐文章