如何存储对变量的 http angularjs 调用响应
How to store http angularjs call response to variable
我正在从 angularJS 函数调用 http get。调用完美运行并获得响应,但我需要将响应存储在变量中,以便我可以在 $http 之外使用它。我试图保留 $scope.data.submissionFileID 但 alert($scope.data.submissionFileID) 说未定义。我也想让这个调用同步。我是新手,能否请您帮忙修改下面的代码?
$scope.openWindow = function (e) {
var url = '/Submission/GetSubmissionFileInfo?' + 'id=' + SubmissionID;
$http.get(url).success(function (response) {
$scope.data.submissionFileID = response; // response is an integer such as 123
});
alert($scope.data.submissionFileID); // This is undefined, what should I do to fix it?
var content = "<h7><b>" + "Created at </b>" + $scope.data.submissionFileID + "</h7><br><br>";
}
需要考虑的是 alert(...)
在异步函数有机会完成之前被调用。一个选项是将响应发送到另一个函数,该函数设置 $scope 变量并执行您可能想要的任何其他操作。
$scope.openWindow = function (e) {
var url = '/Submission/GetSubmissionFileInfo?' + 'id=' + SubmissionID;
$http.get(url).success(function (response) {
$scope.doTheThing(response);
});
}
$scope.data = {}
$scope.doTheThing = function(response) {
$scope.data.submissionFileID = response;
}
在 HTML 模板文件中...
<div ng-if="!data.submissionFileID">Retrieving Data....</div>
<div ng-if="data.submissionFileID">
<h7><b>Created at </b> {{data.submissionFileID}}</h7>
</div>
我正在从 angularJS 函数调用 http get。调用完美运行并获得响应,但我需要将响应存储在变量中,以便我可以在 $http 之外使用它。我试图保留 $scope.data.submissionFileID 但 alert($scope.data.submissionFileID) 说未定义。我也想让这个调用同步。我是新手,能否请您帮忙修改下面的代码?
$scope.openWindow = function (e) {
var url = '/Submission/GetSubmissionFileInfo?' + 'id=' + SubmissionID;
$http.get(url).success(function (response) {
$scope.data.submissionFileID = response; // response is an integer such as 123
});
alert($scope.data.submissionFileID); // This is undefined, what should I do to fix it?
var content = "<h7><b>" + "Created at </b>" + $scope.data.submissionFileID + "</h7><br><br>";
}
需要考虑的是 alert(...)
在异步函数有机会完成之前被调用。一个选项是将响应发送到另一个函数,该函数设置 $scope 变量并执行您可能想要的任何其他操作。
$scope.openWindow = function (e) {
var url = '/Submission/GetSubmissionFileInfo?' + 'id=' + SubmissionID;
$http.get(url).success(function (response) {
$scope.doTheThing(response);
});
}
$scope.data = {}
$scope.doTheThing = function(response) {
$scope.data.submissionFileID = response;
}
在 HTML 模板文件中...
<div ng-if="!data.submissionFileID">Retrieving Data....</div>
<div ng-if="data.submissionFileID">
<h7><b>Created at </b> {{data.submissionFileID}}</h7>
</div>