如何在Ionic框架启动时加载$http service app

How load $http service app on startup in Ionic framework?

本文关键字:加载 http service app 启动 Ionic 框架      更新时间:2023-09-26

如何在Ionic框架启动时加载$http服务应用程序?

//这是一个服务

appModule.factory('NewGames', function($http, $ionicLoading) {
  // JSON Array
  var newGames = [];
  var request = {'searchString' : 'name_fr'};
  $http.get('http://example.com/rest/data.php', { cache: true}, request).success(function(response) {
      newGames = response;
  });

  return {
    all: function() {
      //$ionicLoading.hide(); 
      return newGames;
    }
  }
});

//这是一个控制器

myApp.controller('MainCtrl', function($scope, NewGames, $ionicSlideBoxDelegate, $stateParams, $ionicPlatform) {

  $scope.nextSlide = function() {
    $ionicSlideBoxDelegate.next();
  }
  $scope.newGames = NewGames.all();
});

按下按钮,数据开始加载。如何使数据在启动时加载?

使用此处记录的ionic.Platform.ready()方法

你可以在应用程序的任何地方运行这个方法,可能是工厂或控制器。