javascript - Why use $http in Angular instead of jquery's ajax? -
i don't understand when use angular on jquery ajax requests.
for example, why should use:
function itemlistctrl ($scope, $http) { $http.get(‘example.com/items').success(function (data) { $scope.items = data; } }
instead of
function itemlistctrl ($scope) { $.ajax({type: "get", url: 'example.com/items', success: function (result) { $scope.items = data; } }); }
??
my understanding there couple reasons first preferred:
- $http testable. it's possible stub out backend uses , test $http requests without actually sending requests.
- $http common "stuff" you, such setting content type 'application/json' on post requests.
- $http returns "promise" similar other areas in angular, means .success, .done consistent angular. jquery allows similar, syntax different.
- $http success , error callbacks execute inside of angular. if use jquery, might necessary call $apply, can tricky in cases.
- $http works without jquery. if don't have other reason include jquery, possibly save few k using $http.
- $http shorter. subjective, personally, reads better me.
aside those, though, should able either.
Comments
Post a Comment