2016-08-22 5 views
0

내 HTML에 다음과 같이 두 입력 요소가 있습니다Promise 함수에서 응답을 얻는 방법 .then?

<input type="date" ng-model="calendarStart" class="form-control" style="display: inline-block; width: 180px !important;" /> 
<input type="date" ng-model="calendarEnd" class="form-control" style="display: inline-block; width: 180px !important;" /> 

과의 .js이 : 나는 누나, 그 때는 함수에서 응답 값을 얻을 수있는 방법

$scope.calendarStart = new Date(); 
$scope.calendarEnd = new Date(); 
$scope.calendarEnd.setDate($scope.calendarEnd.getDate() + 7); 

var firstEndDate = new Date(); 
var firstStartDate = new Date(); 

var startData = "calendarStart"; 
var endData = "calendarEnd"; 

$scope.changeCalendarStart = function() {   
    dataService.settings_get(startData).then(function(response) { 
     firstStartDate = response; 
     if (firstStartDate != null) 
      $scope.calendarStart = firstStartDate; 

     return firstStartDate; 
    }); 
    return firstStartDate; 
}; 

은 입력의 값을 변경해야 ?

+0

var firstStartDate를 $ scope.firstStartDate로 변경하고 범위에서 variable을 사용하십시오. – KoIIIeY

답변

0

약속과 해당 연결을 이해하지 못했습니다. 비동기식이므로 호출자가을 가져올 때 을 알지 못하기 때문에 호출자에게 "firstStartDate"값을 반환 할 수 없습니다.

그래서, 이런 일에 changeCalendarStart을 변경해보십시오 :

$scope.changeCalendarStart = function() {   
    return $q.function(resolve, reject){ 
    dataService.settings_get(startData).then(function(response) { 
     firstStartDate = response; 
     if (firstStartDate != null) 
      $scope.calendarStart = firstStartDate; 

     resolve(firstStartDate); //from here, we are going to the then function of the caller below 
    }); 
    } 
}; 

과 약속, 예를 들어, 다음과 같이 사용할 이 방법 :

$scope.changeCalendarStart().then(function(resultValue){ //this inner function it's called by the row with resolve(firstStartDate) above 
    $scope.myResultValue = resultValue; 
} 

나는 당신이 그들을 완전히 이해하기위한 약속에 관한 지침서를 따르는 것이 좋습니다. This이 좋은 예입니다.

관련 문제