Javascript 如何在angularjs中调用sync和async?
我想自定义同步和异步在angularjs如下Javascript 如何在angularjs中调用sync和async?,javascript,angularjs,angularjs-scope,Javascript,Angularjs,Angularjs Scope,我想自定义同步和异步在angularjs如下 var async = function (url, data, action, callback) { $.ajax({ url: global.baseURL + url, type: action, data: data, async: true,//or false
var async = function (url, data, action, callback) {
$.ajax({
url: global.baseURL + url,
type: action,
data: data,
async: true,//or false
success: function (data) {
if (data !== undefined && data !== null) {
callback(data);
}
}
});
};
在AngularJs中,如下代码所示
$http({ method: 'GET', url: baseURL + 'Api/MobilePref/Get/'+uid
})
.success(function (data, status, headers, config) {
})
.error(function (data, status, headers, config) {
//TODO: handl error.
});
有人能告诉我如何在angularjs中设置同步和异步调用吗?您可以这样做:
function callAjax(url, callback){
var xmlhttp;
xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function(){
if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
callback(xmlhttp.responseText);
}
}
xmlhttp.open("GET", url, true); // the flag true tell if this is async or not and then you can call $scope.$apply for angular to know.
xmlhttp.send();
}
看来这是不可能的。同步调用在$http中是不可能的。后端默认设置为异步。同步调用导致阻塞,阻塞是不好的!有人能给出解决方法吗?xmlhttp.open(“GET”,url,false)//这意味着我们可以说这是一个“同步”呼叫?不,设置为true就是同步呼叫