2017-09-19 3 views
0

$ scope.init()에서 $ scope.email에 액세스하려면 어떻게해야합니까? 현재 그것은 나를 정의하지 않고 있습니다.Anglesjs의 init 함수에서 .then에 설정된 값에 액세스하는 방법은 무엇입니까?

 app.controller('someCtrl', 
     [$scope,$http,$location,function($scope,$http,$location){ 
     $scope.init = function(){ 
      $scope.func1(); 
      console.log($scope.email); // giving undefined 
    } 

$scope.func1 = function(){ 
     var data = { 
     'Content-Tye' : 'application/json', 
     'some-token' : $cookies.get('sometoken') 
    }; 
    $http({ 
     method : 'GET', 
     url : some url, 
     headers = data, 
     timeout: 50000 

    }).then(function(successData){ 
    $scope.email = successData.entity.email; 
    }).then($http({ 
    method:'GET', 
    url: some url, 
    headers: data, 
    timeout:50000 
    }) 
    .then ..) 

    } 

    }]) 

나는 일련의 .the 나를 혼란스럽게합니다. $ scope.email이 설정 될 때까지 어떻게 기다려야합니까? 내 console.log 문은 설정되기 전에 실행됩니다. 어떻게 기다려야합니까? 나는 약속에 대해 읽었지 만, 혼란스러워하는 것은 나. 먼저 콘솔 로그를 완성 할 때까지 기다리고 싶습니다.

답변

1

기다릴 싶은 경우에, FUNC1이 약속을 반환해야합니다, 그리고 당신이 당신의 .then 기능의 비동기 경우,

$scope.init = function(){ 
    $scope.func1() 
    .then(function() { // <--- using .then 
     console.log($scope.email); 
    }); 
} 

$scope.func1 = function(){ 
    var data = { 
    'Content-Tye' : 'application/json', 
    'some-token' : $cookies.get('sometoken') 
    }; 
    return $http({ // <--- notice the return 
    method : 'GET', 
    url : some url, 
    headers = data, 
    timeout: 50000 
    }).then(function(successData){ 
    $scope.email = successData.entity.email; 
    }).then(
    ... etc ... 
); 
}); 

또한 그 약속의 .then 기능을 사용해야하고 있습니다 그들을 기다리기를 원하고, 그들이 약속을 되 돌리는 지 확인하십시오.

+0

감사 니콜라스. 이 질문에 대한 답변입니다. –

0

니콜라스 (Nicholas)는 거의 저를 때려 눕히고 그의 대답은 효과가있을 것입니다. 그러나 다른 사람이 신경 쓰지 않고 이메일이 설정되었는지 확인하고 싶다면이 방법이 효과적입니다. 당신은 다음과 같은 코드를 시도 할 수 있습니다

app.controller('someCtrl', 
    [$scope,$http,$location,function($scope,$http,$location){ 
    $scope.init = function(){ 
     $scope.func1().then(function() { 
     console.log($scope.email); // giving undefined 
     }); 
} 

$scope.func1 = function(){ 
var deferred = $q.defer(); 
var data = { 
    'Content-Tye' : 'application/json', 
    'some-token' : $cookies.get('sometoken') 
}; 

$http({ 
    method : 'GET', 
    url : some url, 
    headers = data, 
    timeout: 50000 

    }).then(function(successData){ 
    $scope.email = successData.entity.email; 
    deferred.resolve(); 
    }).then($http({ 
    method:'GET', 
    url: some url, 
    headers: data, 
    timeout:50000 
    }) 
    .then ..) 

    } 

    return deferred.promise; 
}]) 
+0

나는이 옵션을 시도하고있다. 여기에 업데이트가 완료 될 것이다. –

0

, 대답에 대한

app.controller('someCtrl', [$scope,$http,$location,function($scope,$http,$location){ 
    $scope.init = function(){ 
     var data = { 
     'Content-Tye' : 'application/json', 
     'some-token' : $cookies.get('sometoken') 
     }; 
     getEmail(data).then(function(response){ 
      $scope.email = response.data; 
      console.log($scope.email); 
      $scope.func1(data); 
     }); 
    } 
    function getEmail(data){ 
     return $http({ 
      method : 'GET', 
      url : some url, 
      headers = data, 
      timeout: 50000 
     }).then(function(response){ 
      return response.data.email; 
     }); 
    } 
    $scope.func1 = function(data){ 
     $http({ 
      method : 'GET', 
      url : some url, 
      headers = data, 
      timeout: 50000 
     }).then(function(response){ 
      $http({ 
       method:'GET', 
       url: some url, 
       headers: data, 
       timeout:50000 
     }).then(function(response){ 
      // do something... 
     }); 
    }; 
}]);