2017-03-09 10 views
1

メソッド1は、2番目のメソッド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を渡すことができるし、それを上書きすることにより、関数の中にそれを変更しません。

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

しかし、これは理想的なワークフローではありません。

関連する問題