如何在angularjs中调用sync和async?
How to call sync and async in angularjs?
我想在 angularjs 中自定义同步和异步,如下所示。
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();
}
我想在 angularjs 中自定义同步和异步,如下所示。
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();
}