How to create a AngularJS promise from a callback-based API

前端 未结 2 1878
执念已碎
执念已碎 2020-12-02 02:46

wifiservice.js:

angular.module(\'app.WifiServices\', [])
    .factory(\'WifiService\', function(){
        var unique_array = angular.fromJson(\'[]\');

             


        
相关标签:
2条回答
  • 2020-12-02 03:10

    How to create a AngularJS promise from a callback-based API

    To create an AngularJS promise from a callback-based API such as WifiWizard.connectNetwork, use $q.defer:

    function connectWifi(wifi_ssid) {
       var future = $q.defer();
       var win_wifi = future.resolve;
       var fail_wifi = future.reject;
       WifiWizard.connectNetwork(wifi_ssid, win_wifi, fail_wifi);
       return future.promise;       
    };
    

    The above example returns a $q Service promise that either resolves or rejects using the callbacks from the API.

    0 讨论(0)
  • 2020-12-02 03:15

    Well, I came up with something different:

    var unique_array = [];
      $scope.wifiList = [];
      $ionicLoading.show({
        template: "Scanning surrounding AP's..."
      });
    
     window.setTimeout(function() {
      $scope.wifiList = WifiService.list();
      // alert($scope.wifiList);
      while ($scope.wifiList == []) {
        console.log('Scanning...');
      }
      $scope.$apply();
      $ionicLoading.hide();
     }, 5000);
    

    What I realize is that the scanning starts once I load the view. So, I added an IonicLoader to force the user to wait, and not be able to press any buttons till the scan is finished. So no function shall wait one another. Not quite code-wise correct, but it does what I need.

    0 讨论(0)
提交回复
热议问题