我是angularjs中的新成员,并且存在与angular中的工作同步问题。 这里是我的代码,要填写的控制变量与温控功能的结果,现在它返回一个空的对象或undfined:运行angularjs代码同步并为函数结果等待varibale
我想使用的服务和使用时,他们已经准备好结果的解决方案,又该我做?
App.service("UserService",['$location',"$http","$timeout",function($location,$http,$timeout){
var User={};
var CheckAuthenticationURL="/fa/home/IsAuthenticate";
if($location.protocol() == "https")
{
CheckAuthenticationURL="https://www.mytest.org/fa/home/IsAuthenticateSecure";
}
var UpdateUserData= function(){
return $http.get(CheckAuthenticationURL)
.success(function(data, status, headers, config) {
User=data;
})
};
var getUser= function()
{
UpdateUserData();
return User;
}
return{
UpdateUserData: UpdateUserData,
getUser: getUser
};
}]);
App.controller('GeneralCtrl',['$scope','$http','UserService','$interval', function($scope,$http,UserService,$interval) {
$scope.User={};
$scope.User=UserService.getUser();
}]);
'$ http.get'是异步的,你不能让它变成同步的。你不应该试图让它变成同步,你应该等待UserService在使用之前被填充。你正在使用'angular-route','angular-route2'或'ui-router'吗? – Camusensei
@Camusensei:那么,它有什么解决方案?它可以随时有它,但是当页面加载时,它将值设置为未定义,并且在结果出现后没有更新 –
不,我没有使用它们中的任何一个 –