gpt4 book ai didi

javascript - Angularjs $http VS jquery $.ajax

转载 作者:可可西里 更新时间:2023-11-01 01:38:48 26 4
gpt4 key购买 nike

我可以像在 jQuery 的 $.ajax 中那样在 Angularjs $http 中设置 context 吗?

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;
});
});
});

此外,在 jQuery 的 $.ajax 中还有更多的回调,比如 .done.promise,我可以使用它们来操作context 就像下面这样,我想知道我是否可以在 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

  • 并且 $http 不支持 async:false

  • $.ajax 支持async:false

你可以通过这种方式使用angular.js来完成

$http.get('server.php').success(function(response) {
$scope.contacts = response.data;
}).error(function(error)
{
//some code
});

但是 async: true,angular.js 中不被支持。

如果需要停止异步回调,那么必须使用$.ajax方式

更多详细信息请参阅此讨论:from jquery $.ajax to angular $http

编辑:

如何在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();
});

关于javascript - Angularjs $http VS jquery $.ajax,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20860207/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com