简体   繁体   中英

Callback/promises for $cordovaDialogs in function

$cordovaDialogs itself has a promises as follow:

$cordovaDialog.alert('message', 'title', 'OK').then(function() {
    $state.go('app.nextPage');
});

It working perfectly fine if i use directly, but now I am trying to encapsulate it into a function as follow:

app.factory('AppCommon', function ($cordovaDialogs) {

    var alert = function(message, title, confirmButton) {
        $cordovaDialogs.alert(message, title, confirmButton)
    }

    return {
        alert: alert
    }
});

Now I am trying to use it this way but it fails. What can i do to make $state.go to execute only if the alert box is confirmed?

AppCommon.alert('message', 'title', 'OK').then(function() {
    $state.go('app.nextPage');
});

Just return the promise you are trying to do .then on from alert() function. Since $cordovaDialogs.alert() returns a promise, you need to return it.

app.factory('AppCommon', function ($cordovaDialogs) {

  var alert = function(message, title, confirmButton) {
    return $cordovaDialogs.alert(message, title, confirmButton)
  }

  return {
    alert: alert
  }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM