代码之家  ›  专栏  ›  技术社区  ›  Dataman In Time

使用对话框确认Nativescript angular

  •  0
  • Dataman In Time  · 技术社区  · 7 年前

    我有一个utils类,它包含showMsg函数来使用ui/dialogs

    showMsg(titleTxt: string, msg: string, btnTxt: string, type: string){
     switch(type){
       case 'confirm':
        dialogs.confirm({
              title: titleTxt,
              message: msg,
              okButtonText: btnTxt,
              cancelButtonText: 'No'
          }).then((result) => {
              console.log(titleTxt + " Dialog closed!" + result);
              return result;
        });
          break;
      default:
        dialogs.alert({
              title: titleTxt,
              message: msg,
              okButtonText: btnTxt
          }).then(() => {
              console.log(titleTxt + " Dialog closed!");
              return true;
        });
    }
    

    }

    问题是,当我从另一个组件调用此函数以进行确认对话框时,该组件在继续之前不会等待对话框的结果。我希望它根据用户响应有条件地继续。

    编辑:

    新的showMsg函数

    showMsg(titleTxt: string, msg: string, btnTxt: string, type: string){
       switch(type.toLowerCase()){
      case 'confirm':
        return dialogs.confirm({
              title: titleTxt,
              message: msg,
              okButtonText: btnTxt,
              cancelButtonText: 'No'
          }).then((result) => {
              console.log(titleTxt + " Dialog closed!" + result);
              return result;
        });
      default:
       return dialogs.alert({
              title: titleTxt,
              message: msg,
              okButtonText: btnTxt
          }).then(() => {
              console.log(titleTxt + " Dialog closed!");
              return true;
        });
      }
    }
    

    现在对showMsg函数的调用如下

            this.myutil.showMsg('Update Cleaning Status', 'Are you Sure?', 'Yes', 'Confirm').then((result)=> { 
                console.log('In then: ' + result);
                alert(result);
             });
    

    不幸的是,在调用函数之后,我们不能有任何代码,因为它在中发生的事情之前执行。然后()

    1 回复  |  直到 7 年前
        1
  •  0
  •   Manoj    7 年前

    这绝对不会等待,因为这是一个承诺。你必须使用 async/await helpers 或等待 showMsg 例如,要完成 showMsg().then((result)=> { //anything you want to do after dialog is closed }) 在组件中。