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");
}
});
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.
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.
EDIT: based on the the answer, it seems that AngularJS is not faster than jQuery since it also uses a version of jQuery (jqLite).
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.
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();
});
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With