2016-06-21 2 views
0

저는 아약스 (get) 요청을하고 각도 "jobList"서비스에서 json 데이터를 사용하여 약속을 얻습니다.

그런 다음 획득 한 데이터로 범위를 업데이트합니다. 하지만 내 문제는 범위 변수 'X'를 업데이트하려면 변수 "readX"(벨로우 참고) 당 함수를 만들어야한다는 것입니다.

다음 코드의 마지막 함수와 같이 매개 변수를 추가하는 방법이 있습니까?

app.controller("JobListController", ['$scope', '$timeout', 'jobList', 
    function ($scope, $timeout, jobList) { 
     var readList = function (response) { 
      if (response) { 
       $timeout(function() { 
        $scope.list = response; 
        $scope.$apply(); 
       }); 
      } 
     }; 
     var readFamilies = function (response) { 
      if (response) { 
       $timeout(function() { 
        $scope.allFamilies = response; 
        $scope.$apply(); 
       }); 
      } 
     }; 
     var readRegions = function (response) { 
      if (response) { 
       $timeout(function() { 
        $scope.allRegions = response; 
        $scope.$apply(); 
       }); 
      } 
     }; 
     // !!! ----- HERE ------------- !!! 
     var readSomething = function (response, something) { 
      if (response) { 
       $timeout(function() { 
        $scope[something] = response; 
        $scope.$apply(); 
       }); 
      } 
     }; 

     jobList.get().then(readList); 
     jobList.getAll("allFamilies").then(readFamilies); 
     jobList.getAll("allRegions").then(readRegions); 
    }]); 

답변

1

처음에는 콜백 함수를 단순화 할 수 있습니다. 콜백이 각도 내에서 발생하면 ($http을 사용하는 경우) $timeout 호출이나 $scope.$apply() 호출이 필요하지 않습니다. 또한 데이터를 반환하는 경우에만 성공하도록 서비스를 작성하고 실패하면 약속을 거부하며 if이 필요하지 않도록 각 콜백이 할당 될 수 있습니다.

약속을 되풀이하는 여러 통화를하는 경우 통화를 함께 축소 할 수 있습니까?

$q.all([jobList.get(), jobList.getAll("allFamilies"), jobList.getAll("allRegions")]) 
.then(([list, families, regions]) => { 
    $scope.list = list; 
    $scope.allFamilies = families; 
    $scope.allRegions = regions; 
}); 

는 여기 ES6 구문을 사용 : 당신이 간단한 콜백에 대한 속기 표기법을 사용할 수 있도록 babeljs 같은 것을 사용하는 빌드 체인을 설정하는 가치입니다.

그래서
function assignToScope(name) { 
    return success; 

    function success(data) { 
     $scope[name] = data; 
    } 
} 
jobList.get().then(assignToScope('list')); 
jobList.getAll("allFamilies").then(assignToScope('allFamilies')); 
jobList.getAll("allRegions").then(assignToScope('allRegions')); 
0

이 시도 :

jobList.get().then(function (response) { 
    readSomething(response); 
    }); 

그리고 기능 readSomething은 입력과 같이 응답을 할 수 있습니다.

1

데이터를 가져 오기 전에 변수를 scope 변수에 저장할 수 있습니다. 이 같은

뭔가 :

$scope.property = "list"; 
jobList.get().then(readSomething); 

과 기능은 지금 될 것입니다 :

var readSomething = function (response) { 
      if (response) { 
       $timeout(function() { 
        $scope[$scope.property] = response; 
        $scope.$apply(); 
       }); 
      } 
     }; 

PS : 나는 당신이이 같은 뭔가를 클로저를 사용할 수있는 것 같아요

:

var readSomething = function (something) { 
      return function(response){ 
       if (response) { 
        $timeout(function() { 
         $scope[something] = response; 
         $scope.$apply(); 
        }); 
       } 
      } 
     }; 
+0

이 전화 foreach는 기능을 할 필요가, 다음 변수를 설정합니다

당신이 정말로 (그들은 여전히 ​​병렬로 평가)는 콜백을 생성하는 공장을 작성할 수 있습니다 개별적으로 전화를 확인하려면

함수를 호출하십시오 ... – Serge

+0

예, 그렇지 않으면 닫는 방법을 살펴볼 수 있습니다, 나는 내 대답을 편집했습니다 – gaurav5430

+0

어떻게 readSomething을 사용할 수 있습니까? – Serge

관련 문제