15

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

2 Answers 2

23
+50

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(); 
    });
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks. Can I know what are ** for?
@lauthiamkok ** is nothing. it's SO bold line mark. $scope.$apply() means $scope.IsShow=true apply for the Html. But it's maybe don't need.
THanks. I do find jquery method is easier and more straight forward. don't you think?
@lauthiamkok , Yep you can do it jquery. So why you use angular.js? you can do it by using jquery or angular.js . both are best. but angular.js helps to binding fast than jquery. Happy coding :)
could you improve the english grammar in your answer a little bit, would be easier to understand
2

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?

2 Comments

thanks Jeff. I only need to do two things like I do in $.ajax - 1. beforeSend: function() { $(this).html('loading...'); } and 2 - success: function (returndata, status, jqxhr) { $(this).hide().fadeIn(); }.
So... yeah, you should instead use a request and response interceptor and show/hide that based on the number of pending HTTP requests.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.