【发布时间】:2021-07-18 17:12:06
【问题描述】:
我正在从 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>";
}
【问题讨论】:
-
我想让这个调用同步,因为函数的其他部分我想让我的代码等待。
-
你不能,因为网络请求基本上是异步的。
-
我怎样才能让我的代码等待,因为它正在准备内容并且无法等到响应返回。添加了我正在准备的内容和响应。 var content = "
" + "创建于 " + $scope.data.submissionFileID + "
";
标签: angularjs ajax http scope storing-data