gpt4 book ai didi

javascript - $http 变量范围问题

转载 作者:行者123 更新时间:2023-12-02 17:26:24 24 4
gpt4 key购买 nike

$http({
url: "php/myuId.php",
method: "POST",
data: {
'userId': userId,
},
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
}).success(function(data, status, headers, config) {

$scope.myId= data;
//return last inserted id
}).error(function(data, status, headers, config) {
});
//can't use it outside?
$scope.user.push({
"userId": $scope.myId,

});

奇怪的是,当我在 $http 范围之外使用 $scope.myId 时,它并没有改变,所以我无法将最后插入的 id 推送到前端。

最佳答案

问题在于它是异步的,因此在 promise 返回之前您还没有数据。您可以将 $scope.user.push 放入成功回调中,或者使用 $scope.$watch("myId"... 在值更新时进行注册。

关于javascript - $http 变量范围问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23464350/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com