javascript - What is the best practice for multiple $http request in AngularJS? -
For example, I have a controller to show a list view to the user, some columns are needed from the second endpoint of the data, so I wrote with these intuition, can you tell me again How do I work from?
$ http.get ($ scope.urlA) .success (function (res) {$ scope.dataA = res .data; $ http.get ($ scope.urlB) .success ( Function (res) {$ scope.dataB = res.data;}) .error (function (mistake) {if (err) err;}) Panic (function (mistake) {if mistake (mistake);}) ;
Then the best practice is to build two factory methods for $ http.get Http invoice promises itself by $ in fingerprints so that your factory will look like this:
myapp.factory ('getHttpData', function ($ http) {return {getURLA: function ( ) {Return $ http .get ('/ urlA');}, getURLA: Function () {return $ http.post ('/ urlB');}}});
Then you can apply both factory functions in this way:
.controller ('testCtrl', [getHttpData ', function (getHttpData) {GetHttpData.getURLA (). Then (function (data) {// whatever you want to get HttpData.getURLB (). Then (function (Bdata) {// whatever you wish}}}}};});