2017-03-09 63 views
1

方法1完美工作,當我嘗試寫入它作爲一個函數,如第二種方法2,UI網格不更新, 什麼是好辦法呢?

方法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並通過覆蓋修改它裏面的功能。你可以採取更好的辦法是通過更新函數返回承諾對象和grid_update使用此承諾:

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 
    }) 
} 

或者,你仍然可以使你原有的解決方案的工作,如果你是通過$scope.gridOptions到grid_update:

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

和變異這個對象update_from_MySQL

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

但這並不是理想的工作流程。