2016-01-21 45 views
0

我試圖分配一個可變數據之前 - 從我的$ HTTP請求檢索>值。 但是,即使在使用諾言之後,用於該變量的代碼也會首先執行,而不是等待http請求。

我是新來的角,但是我想「承諾」要照顧這。 請建議!

.controller('Gym_DetailsController', function($scope, $http, $state, $timeout, providerdetailservice, $location, $ionicScrollDelegate, $window, $rootScope, $q) { 

    $rootScope.providerid = $state.params.UPNO; 
    $scope.whichProv = $state.params.UPNO; 


    var one = providerdetailservice.getdetails($scope.whichProv); 
    $q.all([one]).then(function(arr) { 
      $scope.providerinfo = arr; 
      console.log(arr[0].data[0].AREA); 
      $scope.image = arr[0].data[0].ProvImage; 
      console.log($scope.image); * * // This gives output as my required answer but SECOND --> Executed Second** 
     }, 
     function(err) { 

     } 
    ). 
    finally(
     function() { 
      //Nothing 
     }) 
    console.log($scope.image); * * // This gives output as undefined FIRST --> Executed first** 
    $scope.items = [{ 
     src: $scope.image, 
     sub: 'This is a <b>subtitle</b>' 
    }] 
}) 
+0

這可能是最好的/最簡單的資源,當它涉及到承諾,它使用漫畫! http://andyshora.com/promises-angularjs-explained-as-cartoon.html – lux

+0

謝謝@lux。這很有用。 – Sanketh

回答

0

你的第二的console.log()第一次運行,因爲它不是你的諾言內。

如果你正在做

$scope.items = [{ 
    src: $scope.image, 
    sub: 'This is a <b>subtitle</b>' 
}]; 

$ scope.image因爲承諾尚未兌現將不可用呢。將此代碼移入promise,它應該可以正常工作。

var one = providerdetailservice.getdetails($scope.whichProv);  
$q.all([one]).then(function(arr){ 
    $scope.providerinfo = arr; 
    console.log(arr[0].data[0].AREA); 
    $scope.image = arr[0].data[0].ProvImage; 
    console.log($scope.image); 

    //now try and access $scope.image 
    $scope.items = [{ 
     src: $scope.image, 
     sub: 'This is a <b>subtitle</b>' 
    }]; 
}, function(err){} 
).finally(function(){ //Nothing }); 
0

這就是我所做的,它工作的很棒。

Service.getData("param1", $scope.otherParam).then(
    function(data){ 
     console.log(data.data); //hey there it is... :) 
     $scope.someOtherfunctionCallback(data.data); 
    } 
).finally(function(){ console.log("finally done with that crap..."); }); 

我的服務:

angular.module('app') 
    .factory('Service',['$http','$q',function($http, $q){ 
var service={}; 
var getReportMenu = function(){ 
     //console.log("getting menu..."); 
     var defer = $q.defer(); 
      $http({ 
       method:'GET',    
       url:'http://localhost:8080/API/PATH',    
       cache:true, 
       dataType:'json'}) 
      .then(
       function successCallback(response){ 
        defer.resolve(response); 
       }, 

       function errorCallback(response){ 
        //TODO: handle this elegantly later. 
        alert("Error!"); 
       }); 
      return defer.promise; 
     } 

return { getReportMenu : getReportMenu }; //Just easy encapsulation. 

} 
}); 
+0

這是從我的工作應用程序快速打耳光。請原諒任何格式或支架問題。它應該讓你指出正確的方向。 – MichaelWClark

0

$q.all([one]).then(function(arr) { 
 
      $scope.providerinfo = arr; 
 
      console.log(arr[0].data[0].AREA); 
 
      $scope.image = arr[0].data[0].ProvImage; 
 
      $scope.items = [{ 
 
      src: $scope.image, 
 
      sub: 'This is a <b>subtitle</b>' 
 
      }] 
 
      console.log($scope.image); * * // This gives output as my required answer but SECOND --> Executed Second** 
 
     }, 
 
     function(err) { 
 

 
     } 
 
    ). 
 
    finally(
 
     function() { 
 
      //Nothing 
 
     }) 
 
    console.log($scope.image); * * // This gives output as undefined FIRST --> Executed first** 
 
    $scope.items = [{ 
 
     src: $scope.image, 
 
     sub: 'This is a <b>subtitle</b>' 
 
    }] 
 

+0

更新$ scope.items = [{src:$ scope.image, sub:'This is a subtitle' }] 在您的承諾中回電 – saikumar