2017-12-18 20 views
0

페이지로드시 데이터베이스에서 데이터를 가져와 $scope.x에 배치합니다. 내 네트워크 탭에서 데이터베이스에서 목록을 가져 오지만 ng-repeat을 사용하여 곧 드롭 다운 목록에서 필요하게 될 변수에로드되지 않습니다. 그러나 페이지를로드 한 후 데이터를 검색하여 $scope.x에 배치하는 버튼을 클릭하면 이미 값이 표시됩니다. 만약 내가 좋아하면, 페이지로드시 값을 얻지 않고 변수에 데이터를 가져 오는 버튼을 클릭하기 만하면 아무것도 얻지 못합니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까.데이터베이스에서 가져온 데이터가 페이지로드시 변수에로드되지 않았습니다.

$scope.addProject = function() { 
    debugger; 
    console.log($scope.clientList); 
    console.log($scope.userList); 
}; 

$scope.getClients = function() { 
    debugger; 
    $http.get('/Clients/GetClients') 
     .then(function (response) { 
      $scope.clientList = response.data; 
     }); 
}; 

$scope.getAllUsers = function() { 
    debugger; 
    $http.get('/User/GetAllUsers') 
     .then(function (response) { 
      $scope.userList = response.data; 
     }) 
}; 

$scope.getClients(); 
$scope.getAllUsers(); 
$scope.addProject(); 

답변

3

같은 데이터를 얻을해야합니다 다음은 내 코드입니다.

는 약속을 반환하는 getClientsgetAllUsers 함수를 수정 :

$scope.addProject = function() { 
    debugger; 
    console.log($scope.clientList); 
    console.log($scope.userList); 
}; 

$scope.getClients = function() { 
    debugger; 
    ̲r̲e̲t̲u̲r̲n̲ $http.get('/Clients/GetClients') 
     .then(function (response) { 
      $scope.clientList = response.data; 
     }); 
}; 

$scope.getAllUsers = function() { 
    debugger; 
    ̲r̲e̲t̲u̲r̲n̲ $http.get('/User/GetAllUsers') 
     .then(function (response) { 
      $scope.userList = response.data; 
     }) 
}; 

그런 약속을 기다리는 $q.all를 사용

var clientsPromise = $scope.getClients(); 
var allUsersPromise = $scope.getAllUsers(); 

$q.all([clientsPromise, allUsersPromise]) 
    .then(function() { 
    $scope.addProject(); 
}); 
+0

angular.js : 14642 ReferenceError : $ q가 정의되지 않았습니다 .. –

+0

'$ http' 서비스를 삽입 한 것과 같은 방식으로'$ q' 서비스를 컨트롤러에 삽입해야합니다 . – georgeawg

+0

와우! 그게 효과가 있었어! 원래 코드에서 작동하지 않는 이유를 설명해 주시겠습니까? 왜 $ q.all 또는 약속을 추가해야합니까? –

0

을 사용하면 서버에서 데이터가 반환되기 전에 addProject 함수가 호출되고이

var getclients= function(){ 

    debugger; 
    console.log($scope.clientList); 
    console.log($scope.userList); 
}; 


getclients(); 
+0

나는 $ scope.getClients를 호출하고()와 $ 범위를. 페이지의 getAllUsers()가 이미로드되었습니다. 내 코드를 확인하십시오. 코드 맨 아래에 있습니다. –

+0

@stackquestions, $ scope.getClients() 대신 getClients()를 사용해야합니다. – Vinoth

+0

이제 제안을 적용 할 때 addProjects가 호출되지 않습니다. 내 업데이트 된 코드를 참조하십시오. –