Angularjs 如果我传递api,Angular服务不会返回任何值
我已经用angular编写了该服务。在浏览器控制台中,数据即将到来,但作为回报,我没有得到[object]Angularjs 如果我传递api,Angular服务不会返回任何值,angularjs,asp.net-mvc,asp.net-web-api,Angularjs,Asp.net Mvc,Asp.net Web Api,我已经用angular编写了该服务。在浏览器控制台中,数据即将到来,但作为回报,我没有得到[object] /// <reference path="angular.min.js" /> var app = angular.module("myapp", []); app.service("myservice", function ($http) { this.myfun = function () { var x; $http.get("ht
/// <reference path="angular.min.js" />
var app = angular.module("myapp", []);
app.service("myservice", function ($http) {
this.myfun = function () {
var x;
$http.get("http://localhost:41173/api/AngularTest/login_valid").success(function (response) {
console.log("all get data",response);
x = response.data
}).error(function (err) {
console.log("all error ", err);
});
debugger;
return x;
}
});
app.controller("myctrl", function ($scope, $http, myservice) {
$scope.transform = function (input) {
$http.get("http://localhost:41173/api/AngularTest/login_valid").then(function (response) {
if (response.data == true) {
$scope.out = myservice.myfun();
}
else {
$scope.out = "Fail";
}
});
}
});
//
var-app=angular.module(“myapp”,[]);
app.service(“myservice”,函数($http){
this.myfun=函数(){
var x;
$http.get(“http://localhost:41173/api/AngularTest/login_valid)成功(功能(响应){
log(“全部获取数据”,响应);
x=响应。数据
}).error(函数(err){
日志(“所有错误”,err);
});
调试器;
返回x;
}
});
app.controller(“myctrl”,函数($scope,$http,myservice){
$scope.transform=函数(输入){
$http.get(“http://localhost:41173/api/AngularTest/login_valid)然后(函数(响应){
如果(response.data==true){
$scope.out=myservice.myfun();
}
否则{
$scope.out=“失败”;
}
});
}
});
你需要遵守承诺。它们是异步回调,您不能从它们返回值,只能返回一个可以解析的承诺。也可以使用.then()
而不是.success()
。下面是它可能的样子:
var-app=angular.module(“myapp”,[]);
app.service(“myservice”,函数($http){
this.myfun=函数(){
返回$http.get(“http://localhost:41173/api/AngularTest/login_valid");
}
});
app.controller(“myctrl”,函数($scope,$http,myservice){
$scope.transform=函数(输入){
$http.get(“http://localhost:41173/api/AngularTest/login_valid)然后(函数(响应){
如果(response.data==true){
myservice.myfun()。
然后(函数(res){
$scope.out=res.data;
},函数(err){
log(“错误:”,err)
})
}否则{
$scope.out=“失败”;
}
});
}
});
异步服务不是这样工作的。您必须返回承诺并管理控制器中的数据
this.myfun = function() {
return $http.get(API).
then(function(response) {
return response.data;
}, function(error) {
return error;
});
};
然后在控制器中
app.controller("myctrl", function ($scope, $http, myservice) {
$scope.transform = function (input) {
$http.get("OTHER API").then(function (response) {
if (response.data == true) {
myservice.myfun().then(function (data) {
$scope.out = data;
},
function(error) {
$scope.out = error;
});
}
else {
$scope.out = "Fail";
}
});
}
});
确保response.data确实返回布尔值
查看控制器,您似乎希望链接http调用
将每个http请求放在一个服务中并使用async await链接它们看起来会更好
如果您有正确的多边形填充:
async transform = function(input) {
var firstData = await myservice.firstCall();
firstData && await myservice.myfun();
}
还可以尝试使用清除$scope的过度使用。是的。这很有效。非常感谢。您能告诉我这背后的原因吗?@Swadesh异步回调可能在几毫秒或几分钟内获取数据,因此您不知道数据何时到达,因此无法将其分配给值。你每次都必须解决它。您可以使用
。然后()
异步/等待使用的ES6承诺没有与AngularJS框架集成。它是javascript。它不需要在任何地方集成。您必须使用$scope.apply()来更新作用域,但它可以工作。