我试图在我的离子应用中显示来自其他功能的警报。这就是我做的事情
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();
}
但是当我点击它时,没有警报显示,我错过了什么?
UPDATE--
以下是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:this.successAlert不是函数
答案 0 :(得分:2)
你需要用paranthesis调用函数
if (response) {
this.successAlert();
} else {
this.failedAlert();
}
修改强>
我已经尝试更改
this.successAlert; to this.successAlert();
但是我收到此错误: TypeError:this.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();
}
);
// ...