问题
My code is some thing
$scope.data = {};
var init = function () {
$http.post('views/Report.aspx/GetMailReport', {})
.success(function (data, status, headers, config) {
$scope.data = JSON.parse(data.d);
})
.error(function (data, status, headers, config) {
$scope.data = status;
})
};
init();
var data = $scope.data;
But the var data is returning empty {}
回答1:
If you want a copy you should do as below... As the http call is asynchronous you assign inside the success callback method
$scope.data = {};
var data;
var init = function () {
$http.post('views/Report.aspx/GetMailReport', {}) .success(function (data, status, headers, config) {
$scope.data = JSON.parse(data.d);
data = $scope.data;
}) .error(function (data, status, headers, config) {
$scope.data = status;
}) };
init();
Edited: Here is the link to a fiddle that I have created.
来源:https://stackoverflow.com/questions/26572610/get-the-value-of-scope-data-in-a-variable-in-same-controlled