如何在Angularjs的init函数中访问.then中设置的值?

时间:2017-09-19 15:41:47

标签: javascript angularjs angular-promise

如何在$ scope.init()中访问$ scope.email。目前它给我未定义。

      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 ..)

  }

  }])

我有一系列的。然后让我感到困惑。我怎么等到我的$ scope.email设置好了?我的console.log语句在设置之前执行。我如何让它等待?我确实阅读过关于承诺的内容,但令我困惑的是多个.then语句。我想等到第一个。然后完成控制台日志。

3 个答案:

答案 0 :(得分: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函数都是异步的,并且您也想等待它们,那么请确保它们返回它们的承诺。

答案 1 :(得分:0)

尼古拉斯勉强打败了我,他的答案会有效,但是如果你不关心对方。那么只是想确保电子邮件已经设定,那么这对你有用。

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;
}])

答案 2 :(得分: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...
       });
    };
}]);