Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angularjs $http VS jquery $.ajax

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"); 
    }
});
like image 925
Run Avatar asked Dec 31 '13 16:12

Run


People also ask

Does AngularJS use AJAX?

Angular vs AJAXAngular is more general whereas AJAX is more specific. In other words, Angular is a big framework whereas AJAX is a JavaScript method that enables asynchronous communication between the database and the server. Angular uses AJAX technology to build single-page applications.

Which is better jQuery or AJAX?

jQuery uses ajax for many of its functions, but it nothing else than a library that provides easier functionality. With jQuery you dont have to think about creating xml objects ect ect, everything is done for you, but with straight up javascript ajax you need to program every single step of the ajax call.

Which is faster jQuery or AngularJS?

EDIT: based on the the answer, it seems that AngularJS is not faster than jQuery since it also uses a version of jQuery (jqLite).

Is angular better than jQuery?

It is well understood that JQuery is best suited for DOM manipulation and Angular JS is best suited for Web application development. Angular JS is used to develop robust applications and to add more functionality or to perform DOM manipulation on the website we can use JQuery.


2 Answers

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(); 
    });
like image 122
Ramesh Rajendran Avatar answered Oct 01 '22 23:10

Ramesh Rajendran


Just use Function.bind() on the function you hand to promise.then to maintain the context you want. For example:

return $http({
    method: 'GET', 
    url:'server.php'
}).then(function(response) {
    $scope.contacts = response.data;
}.bind(this));

However, I'm noticing that your callbacks are all manipulating elements--things you don't need to do in Angular. Is there something specific you're trying to do but unable to with a callback?

like image 41
Jeff Hubbard Avatar answered Oct 02 '22 00:10

Jeff Hubbard