2014-03-13 5 views
0

안녕하세요 저는 각도 j에 익숙하지 않습니다. 여기서는 서버에서 컨트롤러로 json을 가져 와서 데이터를 처리 할 수 ​​있지만 여기서 발생하는 첫 번째 경고는 -11입니다. 그래서 나는 그것을 시도하지만 여전히 내가 미리앵귤러 js에서의 비동기 호출

sampleApp.controller('firstpage', function ($scope, $q, $http, $templateCache, onlinedata, offlinedata) { 
    $scope.message = 'This is Add new order screen'; 
    var defer = $q.defer(); 
    defer.promise.then(function() { 
     $scope.method = 'POST'; 
     $scope.url = 'http://ncrts.com/ncrtssales_compadmin/webservices/user_login'; 
     $scope.fetch = function() { 
      $scope.code = null; 
      $scope.response = null; 
      alert($scope.response + 11) //alert11 
      $http({ 
       method: $scope.method, 
       url: $scope.url, 
       cache: $templateCache 
      }). 
      success(function (data, status) { 
       $scope.status = status; 
       $scope.message = data; 
       alert($scope.message + 1) //alert1 
      }). 
      error(function (data, status) { 
       $scope.data = data || "Request failed"; 
       $scope.status = status; 
      }); 
     }; 
     $scope.fetch(); 
    }) 
     .then(function() { 
     alert($scope.message + 2); //alert 2 
    }) 
    defer.resolve(); 
    //alert($scope.remember) 
}); 
+0

여기서 alert-3은 무엇입니까? –

+0

죄송합니다 경고 1, 편집 됨 – kavinhuh

답변

4

promise로 응답을 처리하려면 해결 및 거부 함수를 사용해야합니다. 약속에 대한 자세한 내용은 AngularJS documentation에서 확인할 수 있습니다. 코드에서 원하는 부분을 얻을 수있는 방법을 약간 변경했습니다.

sampleApp.controller('firstpage', function ($scope, $q, $http, $templateCache, onlinedata, offlinedata) { 
$scope.message = 'This is Add new order screen'; 
var defer = $q.defer(); 

    $scope.method = 'POST'; 
    $scope.url = 'http://ncrts.com/ncrtssales_compadmin/webservices/user_login'; 

    $scope.fetch = function() { 
     $scope.code = null; 
     $scope.response = null; 
     alert($scope.response + 11) //alert11 

     var defer = $q.defer(); 
     $http({ 
      method: $scope.method, 
      url: $scope.url, 
      cache: $templateCache 
     }). 
     success(function (data, status) { 
      //$scope.status = status; 
      $scope.message = data; 
      alert($scope.message + 1) //alert1 
      defer.resolve({ 
       status: status, 
       message: data 
      });     
     }). 
     error(function (data, status) { 
      var data = data || "Request failed"; 
      //$scope.status = status; 
      defer.reject({ 
       status: status, 
       message: data 
      });        
     }); 
     return defer.promise; 
    }; 

    $scope.fetch().then(function (data) { 
     alert('Status: ' + data.status); 
     alert('Message: ' + data.message);    
    }); 
}); 
3

I에 .Thanks를 좀 도와주세요 잇달아 발생 할 작동하지 않는 2 경고가 호출되고 호출되는 최종 경고 1, 나는 약속에 대해 읽어 코드 형식이 매우 혼란 스럽습니다. 비동기 호출을 서비스에 작성한 다음 해당 서비스를 컨트롤러에 주입하는 데 도움이 될 수 있습니다. 이를 염두에두고, AngularJS의 비동기 호출 및 약속과 관련하여 배웠던 몇 가지 일반적인 조언은 다음과 같습니다.

  • 서비스는 기본적으로 지연 약속을 반환해야합니다. 이 약속은 비동기 호출 success()에서 해결되어야합니다.
  • $http()이 반환 된 후, success()이 반환되기 전에, 당신은 미해결 약속이 있습니다. 당신은 당신이 후 약속이 해결 될 때 실행하려는 코드가있는 경우, 당신은 당신이 수신 데이터 (그리고 약속 해결)했습니다 일단 당신이 then() 블록 내에서 코드를 실행할 을 나타 내기 위해 then()를 사용할 필요가 .
  • 데이터가 아직 존재하지 않으므로 오류가 발생합니다.

필요한 코드 작성 전략에 대해 알고있는 것처럼 보이지만 항상 비동기 호출을 서비스로 분리하여 프레임 워크에서보다 쉽게 ​​사용할 수 있도록 도와줍니다. 행운을 빕니다.

+1

실제로 아약스 호출 서비스를 별도로 유지하고 싶지만 여기에 게시하려면, 그것을 혼합, UR 가이드 라인에 대해 매우 감사 유용한 U 감사합니다 – kavinhuh