Angularjs $ HTTP VS jQuery的$。阿贾克斯 [英] Angularjs $http VS jquery $.ajax

查看:96
本文介绍了Angularjs $ HTTP VS jQuery的$。阿贾克斯的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在 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

 < D​​IV 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆