2017-02-09 1 views
0

을 반환하지 않습니다AngularJS와이 내도이다 (긴 변수와 함수 이름에 대한 죄송합니다) defer.promise

<tr ng-repeat="passation in passations " > 
<td ng-init=" passation.stats_quota_campagnes = get_stats_quota_campagnes(passation)">{{passation.stats_quota_campagnes}}</div></td> 
</tr> 

이 결과를 반환하도록되어 내 AngularJS와 함수이다 :

$scope.get_stats_quota_campagnes = function(passation){ 

    var defer = $q.defer(); 
    $http.post('backend/backend.php?action=stats_quota_campagnes',{'id_passation':passation.id_passation}) 
    .success(function(data){ 
     console.log(data); 

      defer.resolve(data); 

    }); 
     return defer.promise; 
} 

console.log (data); 나는 많은 것들을 시도했습니다

Object { 0: "401", Quota: "401" } 

하지만,하지만 내보기에 할당량 값을 얻을 전혀없는이 '메신저 끝에 다시 PHP에서 정확한 데이터를 얻기를 보여! defer.promise 변수는 항상 비어 있습니다. 이유를 모르겠습니다. 내가 뭘 할 수 있니? 을 console.log 그것이 오른쪽 변수는 $의 HTTP에서 오는 작동하고 있음을 보여주고 있지만, 값 대신

{} 

를하지만보기 '아무튼 :

내보기에

, 실제로이 ISEE t 업데이트. $ apply()에 대해 들었지만 사용 방법을 모른다.

답변

0

약속과 관련하여 아무 것도 할 필요가 없으며 .then 콜백에서 변수에 데이터를 할당하면됩니다.

$scope.get_stats_quota_campagnes = function(passation){ 

    $http.post('backend/backend.php?action=stats_quota_campagnes',{'id_passation':passation.id_passation}) 
     .success(function(data){ 
      console.log(data); 
      passation.stats_quota_campagnes = data; 
     }); 
} 

그리고보기에

는 : 어떻게 작동하는지

<tr ng-repeat="passation in passations"> 
    <td ng-init="get_stats_quota_campagnes(passation)"> 
     {{passation.stats_quota_campagnes}} 
    </td> 
</tr> 
+0

ng-repeat, ng-repeat의 각 객체에 대해 함수를 호출해야하기 때문에 변수 이름을 어떻게 알 수 있습니까? – user7370387

+0

@nick 'passation'을 매개 변수로 사용합니다. '.success' 콜백 각도는 검색된 데이터를'passation' 객체에 넣습니다. 그러므로 view에서'passation.stats_quota_campagnes'를 사용할 수 있습니다. – devqon

+0

$ scope.myVariableUsedInView는 ng-repeat 루프이기 때문에 존재하지 않습니다. ng-repeat 중에 업데이트해야하는 여러 passation 변수가 있습니다. $ scope 사용법을 볼 수 없습니다.myVariableUsedInView = data – user7370387

0

할 수 있습니다 만 then 콜백 내부에서 해결 약속 값을 aacces : 귀하의 경우에는

somePromise.then(function(value) { 
    console.log(value) 
}) 

이 같은 수행 할 수 있습니다

$scope.get_stats_quota_campagnes = function(passation){ 

    var defer = $q.defer(); 
    $http.post('backend/backend.php?action=stats_quota_campagnes',{'id_passation':passation.id_passation}) 
    .success(function(data){ 
     console.log(data); 
     $scope.data = data; 
    }); 
} 

을 그리고 당신이 당신의 템플릿에 $scope.data에 액세스 할 수 있습니다

+0

내가 내 NG 반복 여러 passation 개체가보기에 그런

angular.module('app') .controller('myController', ['$scope', 'myService', function ($scope, myService) { //...some other code in your controller. $scope.passation.stats_quota_campagnes = []; $scope.get_stats_quota_campagnes = function (passation, index) { myService(passation).then(successCallback, failCallback); function successCallback(response) { $scope.passation[index].stats_quota_campagnes = response; }; function failCallback(response) { //Do whatever you have to do. } }; //This requires you have passation array initialized. angular.forEach(passation, function(val,index) { $scope.get_stats_quota_campagnes(val,index); }); }]) .service('myService', ['$http', '$q', function ($http, $q) { return function (passation) { var defer = $q.defer(); $http.post('backend/backend.php?action=stats_quota_campagnes', { 'id_passation': passation.id_passation }) .success(function (data) { console.log(data); defer.resolve(data); }); return defer.promise; } }]) 

, 필자는 그것을 시도했지만 각 passation에 어떤 가치를 얻을 수 없습니다 ng-repeat의 객체들 – user7370387

+0

컨트롤러의'passations' 콜렉션을 반복 할 수 있고'pass_duration' 항목을 인수로 전달하는'get_stats_quota_campagne'를 호출 할 수 있습니다. 템플릿에서 이것을 수행 할 필요가 없습니다. –

0

이 없습니다. passation.stats_quota_campagnes 범위를 초기화하려면 컨트롤러에서 서비스를 호출 한 다음 결과를 범위에 할당해야합니다.

함수를 호출하여 업데이트해야

<tr ng-repeat="passation in passations" > 
    <td>{{passation.stats_quota_campagnes}}</td> 
</tr> 
+0

고맙습니다. 나는 이것도 시도해 보겠습니다. – user7370387