Javascript Angularjs$http VS jquery$.ajax

Javascript Angularjs$http VS jquery$.ajax,javascript,ajax,angularjs,jquery,Javascript,Ajax,Angularjs,Jquery,我可以在Angularjs$http中设置context,就像在jQuery的$.ajax中一样吗 define([ 'app' ], function(app) { app.controller("controller1", function($scope, $route, $http) { return $http({ method: 'GET', url: 'server.php' }).t

我可以在
Angularjs$http
中设置
context
,就像在
jQuery的$.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;
        });
    });
});
另外,jQuery的
$.ajax
中还有更多的回调,比如
.done
.promise
,我可以使用它们来操作
上下文
,如下所示,我想知道我是否可以在
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"); 
    }
});

只需在你交给promise的函数上使用
Function.bind()
,然后使用
来维护你想要的上下文。例如:

return $http({
    method: 'GET', 
    url:'server.php'
}).then(function(response) {
    $scope.contacts = response.data;
}.bind(this));
然而,我注意到您的回调都是操纵元素——在Angular中不需要做的事情。您是否有一些特定的尝试,但无法通过回调来完成

两者都是一样的

$http引用自angular.js脚本

$.ajax是从jquery脚本中引用的

  • 并且$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
方式

更多详细信息请参见本讨论:

编辑:

如何在
角度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(); 
    });
xx
$http.get('server.php').success(函数(响应){
$scope.contacts=response.data;
$scope.IsShow=true;
$scope.$apply();
}).错误(函数(错误)
{
$scope.IsShow=false;
$scope.$apply();
});

谢谢杰夫。我只需要像在
$中那样做两件事。ajax
-1
beforeSend:function(){$(this).html('loading…');}
和2-
success:function(returndata,status,jqxhr){$(this.hide().fadeIn();}
。所以。。。是的,您应该改为使用请求和响应拦截器,并根据挂起的HTTP请求数显示/隐藏该拦截器。谢谢。我能知道你在干什么吗?@lauthiamkok
***
什么都不是。这是如此粗体的线条标记
$scope.$apply()
表示对Html应用
$scope.IsShow=true
。但是可能不需要,谢谢。我确实发现jquery方法更简单、更直接。你不觉得吗?@lauthiamkok,是的,你可以用jquery来做。那么为什么要使用angular.js呢?您可以通过使用jquery或angular.js来实现。两者都是最好的。但是angular.js比jquery有助于更快地绑定。快乐编码:)你能稍微改进一下你答案中的英语语法吗?这样会更容易理解