从函数中调用函数

时间:2016-02-06 10:57:53

标签: angularjs ionic-framework

我有这个函数,它加载一个JSON文件并填充范围。 如果失败,它会加载一个ionicPopup,有两个选项,OK和Retry。 我想重新启动重试功能

这是我的代码

$scope.loadEvents = function () {

$http.get('https://api.paris.fr/api/data/1.4/QueFaire/get_activities/?token=[foobar]&cid=3,9,18,20,16,10,4,37,1,29,30,11,42,25,5,33,8,39,6,40,17,38,34,32,2,44,23&tag=6,4,7,44,2,45,48&created=0&start=0&end=0&offset=1&limit=50',{header : {'Content-Type' : 'application/json; charset=UTF-8'}})
.success(function (data) {
  $ionicLoading.hide();
  $scope.products = data.data;
  var randomProduct = Math.round(Math.random() * ($scope.products.length - 1))
  $scope.currentProduct = angular.copy($scope.products[randomProduct]);
  $scope.currentProduct.image = $scope.checkhttp();
})
.error(function (data, status, headers, config) {
  $ionicLoading.hide();
  $scope.errorMessage = "Couldn't load the list of events, error # " + status;
  var alertPopup = $ionicPopup.confirm({
    title: 'Could not load list of events, error #0',
    template: '',
    buttons: [
      {text: 'OK'},
      {
        text: '<b>Retry</b>',
        type: 'button-positive',
        onTap: function(e) {
            console.log("Reessayer");

            //retry loadEvents()

            return false;
        }}]
      });
                console.log($scope.errorMessage);
    });
  }
  $scope.loadEvents();

1 个答案:

答案 0 :(得分:0)

我这样说:如果用户点击OK则返回true,如果他点击重试则为false

然后alertPopup检查承诺并因此采取行动

.error(function (data, status, headers, config) {
  $ionicLoading.hide();
  $scope.errorMessage = "Couldn't load the list of events, error # " + status;
  var alertPopup = $ionicPopup.confirm({
    title: $scope.errorMessage,
    template: '',
    buttons: [
      {text: 'OK',
      onTap: function(e) {
          return true;
        }
      },
      {
        text: '<b>Retry</b>',
        type: 'button-positive',
        onTap: function(e) {
            console.log("Reessayer");

            return false;
        }}]
      });
      alertPopup.then(function(res) {
          if(!res)
            $scope.loadEvents();
          else {
            console.log("stop");
          }
        });
                console.log($scope.errorMessage);
    });

  }
  $scope.loadEvents();