Javascript 如何在函数中将$scope.gridOptions.data作为参数传递
方法1工作得很好,当我尝试将其作为第二种方法2中的函数编写时,ui网格没有更新, 这样做的好方法是什么 方法1:Javascript 如何在函数中将$scope.gridOptions.data作为参数传递,javascript,angularjs,node.js,Javascript,Angularjs,Node.js,方法1工作得很好,当我尝试将其作为第二种方法2中的函数编写时,ui网格没有更新, 这样做的好方法是什么 方法1: $scope.grid_update= function () { $http.get('/get_data').success(function (data) { $scope.gridOptions.data = data; }).error(function () {alert("Error")}); }; 方法2 // Functio
$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);
};
您将无法传递整个$scope.gridOptions.data并通过覆盖它在函数中对其进行修改。您可以采取的更好的方法是从update函数返回promise对象,并在grid\u update中使用此promise:
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.grid_update = function() {
update_from_MySQL('/get_data', $scope.gridOptions)
};
或者,如果要将$scope.gridOptions
传递到grid\u 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.grid_update = function() {
update_from_MySQL('/get_data', $scope.gridOptions)
};
并在update\u from\u MySQL
中对该对象进行变异:
$http.get(URL).success(function (data) {
control.data = data
})
但这不是理想的工作流。您将无法传递整个$scope.gridOptions.data并通过覆盖它在函数内部对其进行修改。您可以采取的更好的方法是从update函数返回promise对象,并在grid\u update中使用此promise:
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.grid_update = function() {
update_from_MySQL('/get_data', $scope.gridOptions)
};
或者,如果要将$scope.gridOptions
传递到grid\u 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.grid_update = function() {
update_from_MySQL('/get_data', $scope.gridOptions)
};
并在update\u from\u MySQL
中对该对象进行变异:
$http.get(URL).success(function (data) {
control.data = data
})
但这并不是理想的工作流