离子2警报问题

时间:2016-08-04 07:02:32

标签: angular alert ionic2

基本警报存在一个小问题。所以这是我的情景。 - >我单击页面上的提交按钮,如果没有返回记录,它将给我一个警告消息。我点击确定即可解雇。 - 工作良好。 - >我通过提供无效输入第二次重复相同的情况,这样我就不会得到记录,应用程序再次给我相同的警报。我点击“确定”即可关闭警报 - 不能正常工作。 可以对此有所帮助吗? 这是我获取记录的代码:

fetchGroupMembers(form) {
    //referring this to obj because scope of this is confusing in callback functions
    var obj = this;
    //creating loader
    let loading = Loading.create({
      content: "Please wait...",
    });
    //Showing loader on current screen
    obj.nav.present(loading);
    //Send message to server to fetch the group members
    obj.myGlobals.socket.emit('fetchGroupMembers', { groupCode: form.controls['groupCode'].value });
    obj.myGlobals.socket.on('groupMembers', function (result) {
      while (obj.students.pop()); //removing all elements from array of students
      //fetching each record and creating student
      result.forEach(function (record) {
        obj.students.push(new Student(record));
      });
      //on successfull fetch dismiss the loader
      loading.dismiss();
      if (obj.students.length > 0) {
        //set students to global
        obj.myGlobals.students = obj.students
        //navigating to next page with parameters
        obj.nav.push(HostPage, {
          Students: obj.students
        });
        console.log(obj.nav);
      } else {
        //creating alert  
        obj.doAlert();
      }
    });

  }
  doAlert() {
    let alert = Alert.create({
      title: 'No Student Found!!',
      subTitle: "Please check group code. can't find students!!",
      buttons: ['OK']
    });
    this.nav.present(alert);
  }
}

如果我点击按钮调用doAlert()。它就像魅力一样。我无法弄清楚为什么它在我的场景中不起作用的问题。 任何帮助,将不胜感激。 谢谢,

1 个答案:

答案 0 :(得分:2)

正如Alert类docs中所讨论的那样,正确的方法是等待Alert.dismiss()调用返回的promise来解决:

public TEST = () => {
    let alert1 : Alert = Alert.create({
    title: 'Prompt 1',
    message: "First",
    buttons: [{
      text : 'OK',
      handler : () => {
        console.log("First OK");
        alert1.dismiss().then(() => { // wait for the previous transition to finish or the following alert will malfunction
          let alert2 : Alert = Alert.create({
            title   : 'Prompt 2',
            message : "Second",
            buttons : [{
              text    : 'OK',
              handler : () => {
                console.log("Second OK");
                alert2.dismiss(); // DISMISSING MANUALLY
              }
            }]
          });
          this.nav.present(alert2);
        });
      }
    }]
  });
  this.nav.present(alert1);
};