2016-08-22 5 views
0

私はHTMLで次のように2入力要素を持っている:プロミス関数からの応答を得るには?

<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を持っている:私はCAZ、.then関数から応答値を取得できますか

$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に変更し、scopeで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 
    }); 
    } 
}; 

と、例えば、約束のようにそれを使用しますこの方法で:

​​

私はあなたにそれらを完全に理解する約束のチュートリアルに従うことをお勧めします。 Thisはその良い例です。

関連する問題