Angularjs $ HTTP VS jQuery的$。阿贾克斯 [英] Angularjs $http VS jquery $.ajax
问题描述
我可以在 Angularjs $ HTTP
设置上下文
就像我们可以在做jQuery的$就
?
定义([
应用程序
],功能(程序){
app.controller(控制器1,函数($范围,$路径,$ HTTP){
返回$ HTTP({
方法:GET,
网址:server.php
})。然后(功能(响应){
$ scope.contacts = response.data;
});
});
});
另外,还有一些在jQuery的 $。阿贾克斯
,像 .done
, .promise
,我可以用它来操纵上下文
这样的下面,我不知道如果我能在 Angularjs
?
$。阿贾克斯({
键入:GET,
数据类型:HTML,
网址:server.php,
背景:$(#集装箱),
异步:真正的,
beforeSend:函数(){
$(本)。html的(载入中...);
},
成功:函数(returndata,状态,jqxhr){
$(本)。html的(returndata).hide()淡入()。
},
}),失败(函数(){
警报(错误);
})。完成(功能(returndata){
},
。总是(函数(){
警报(完成);
}
});
两者都是相同的。
$ HTTP从angular.js脚本简称
$。AJAX是从jQuery脚本简称
-
和$ HTTP不支持
异步:假
-
$ AJAX支持
异步:假
您可以通过这种方式使用 angular.js
做
$ http.get('server.php)。成功(函数(响应){
$ scope.contacts = response.data;
})错误(功能(错误)
{
//一些code
});
但异步:真正的,
不支持 angular.js
如果你需要停止异步回调,则必须使用 $。阿贾克斯
办法
更多详细信息,请参阅该讨论:从jQuery的$就到角$ HTTP
编辑:
如何显示隐藏的角JS
< DIV NG秀=IsShow> XX< / DIV>
$ http.get('server.php)。成功(函数(响应){
$ scope.contacts = response.data;
$ scope.IsShow = TRUE;
$范围$适用()。
})错误(功能(错误)
{
$ scope.IsShow = FALSE;
$范围$适用()。
});
Can I set context
in Angularjs $http
just like we can do it in jQuery's $.ajax
?
define([
'app'
], function(app) {
app.controller("controller1", function($scope, $route, $http) {
return $http({
method: 'GET',
url: 'server.php'
}).then(function(response) {
$scope.contacts = response.data;
});
});
});
Also, there are more callbacks in jQuery's $.ajax
, like .done
, .promise
which I can use them to manipulate the context
like this below, I wonder if I can do the same in Angularjs
?
$.ajax({
type: "GET",
dataType: "HTML",
url: 'server.php',
context: $("#container"),
async: true,
beforeSend: function() {
$(this).html('loading...');
},
success: function (returndata, status, jqxhr) {
$(this).html(returndata).hide().fadeIn();
},
}).fail(function() {
alert("error");
}).done(function(returndata) {
},
.always(function() {
alert("complete");
}
});
Both are same
$http is referred from angular.js script
$.ajax is referred from jquery script
and $http does not support
async:false
$.ajax supports
async:false
You can do it by using angular.js
in this way
$http.get('server.php').success(function(response) {
$scope.contacts = response.data;
}).error(function(error)
{
//some code
});
but async: true,
is not supported in angular.js
.
If you need stop asynchronous callback, then you must use $.ajax
way
More details please see this discussion : from jquery $.ajax to angular $http
Edit:
How to show hide in angular js
<div ng-show="IsShow">xx</div>
$http.get('server.php').success(function(response) {
$scope.contacts = response.data;
$scope.IsShow=true;
$scope.$apply();
}).error(function(error)
{
$scope.IsShow=false;
$scope.$apply();
});
这篇关于Angularjs $ HTTP VS jQuery的$。阿贾克斯的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!