gpt4 book ai didi

javascript - angularjs 工厂在 firefox 中显示无法访问的代码错误

转载 作者:行者123 更新时间:2023-11-30 12:18:00 31 4
gpt4 key购买 nike

我正在设置一个 Angular 应用程序,它有一个工厂可以将数据发布到服务器

angular.module('new-ticket-app')
.factory('CreateTicketFactory', function($http, $rootScope){
var CreateTicketFactory = {
async: function($params) {
return $http({
headers: {'Content-Type': 'application/json','X-Auth-Token': $rootScope.access_token},
url: $rootScope.resourceurl+'/server',
method: "POST",
data: $params,
})
.then(function (response) {
// The return value gets picked up by the then in the controller.
return response.data;
});
// Return the promise to the controller
return promise;
}
};
return CreateTicketFactory;
});

Controller 代码是

CreateTicketFactory.async($params).then(
function(d) {
console.log(d);
},
function(d) {
console.log(d);
}
);

上面的代码在 firefox 中显示了unreachable code 错误,并且在第一次加载应用程序时无法正常工作。

最佳答案

您在一个函数中有三个returnreturn $http 行将始终在您的 return promise 行之前返回,因此它是无法访问的代码。如果你想返回 $http 调用本身返回的 promise ,那么只需执行 return $http({... parameters }) 没有 .then 并且没有 return promise 最后。

关于javascript - angularjs 工厂在 firefox 中显示无法访问的代码错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31877036/

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