2017-03-09 2 views
1

방법 1은 두 번째 방법 2처럼 ui-grid가 함수로 쓰려고하면 완벽하게 작동합니다. 무엇입니까? 그렇게하는 좋은 방법?

방법 1 :

$scope.grid_update= function() { 

    $http.get('/get_data').success(function (data) { 

     $scope.gridOptions.data = data; 

    }).error(function() {alert("Error")}); 
}; 

방법 2

// Function declaration 
update_from_MySQL = function (URL,control) { 

    $http.get(URL).success(function (data) { 
     control=data; 

    }).error(function() {alert("Error");}); 
}; 

//Call function 
$scope.grid_update =function({ 
update_from_MySQL('/get_data',$scope.gridOptions.data); 
}; 

답변

0

당신은 전체 $ scope.gridOptions.data 통과 할 수 및 덮어 쓰기하여 함수 내에서이를 수정하지 않습니다 .

$scope.grid_update = function() { 
    update_from_MySQL('/get_data', $scope.gridOptions) 
}; 
: 당신이 grid_update에 $scope.gridOptions를 통과한다면

function update_from_MySQL (URL) { 
    return $http.get(URL) 
    .then(function (response) { 
     return response.data 
    }) 
}; 

$scope.grid_update = function() { 
    update_from_MySQL('/get_data') 
    .then(function (data) { 
     $scope.gridOptions.data = data 
    }) 
} 

을 또는, 당신은 여전히 ​​원래의 솔루션 작품을 만들 수 있습니다 : 당신이 취할 수있는 더 좋은 방법은 업데이트 기능에서 약속 개체를 반환하고 grid_update이 약속을 사용하는 것입니다

update_from_MySQL에서이 개체 변이 :

$http.get(URL).success(function (data) { 
    control.data = data 
}) 

을하지만 이것은 이상적인 워크 플로우를하지.

관련 문제