$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/