gpt4 book ai didi

javascript - $scope.$apply 返回错误 undefined is not a function

转载 作者:行者123 更新时间:2023-11-28 08:17:10 24 4
gpt4 key购买 nike

我在 ng-click 中运行此函数,但收到错误 TypeError: undefined is not a function on this line : $scope.$apply(function () {

app.controller('MainControl', function($scope, $http){

$scope.add = function(){

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

$scope.$apply(function () {
$scope.currentId = data;
});

}).error(function (data, status, headers, config) {});

$scope.tabs.push({
"currentId": $scope.currentId
});

}

});

最佳答案

这里不需要

$scope.$apply() 。移动

$scope.tabs.push({
"currentId": $scope.currentId
});

到http promise 的成功回调

app.controller('MainControl', function($scope, $http){

$scope.add = function(){

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

$scope.currentId = data;
$scope.tabs.push({
"currentId": $scope.currentId
});

}).error(function (data, status, headers, config) {});



}

});

关于javascript - $scope.$apply 返回错误 undefined is not a function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23464714/

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