javascript - Making ajax calls to third party APIs in ionic services.js file? -
started using ionic today , ran small bump. services.js file has comment // might use resource here returns json array
i want make ajax call api don't know how declace custom resource in services.js file. receive json array call , work list thre on.
question: how declare custom variable list makes ajax call third pary api?
angular.module('starter.services', []) .factory('chats', function() { // might use resource here returns json array var popularurl = 'some_api_rul'; var xhttp = new xmlhttprequest(); xhttp.onreadystatechange = function() { if (xhttp.readystate == 4 && xhttp.status == 200) { var chats = xhttp.responsetext; } }; xhttp.open("get", popularurl, true); xhttp.send(); return { all: function() { return chats; }, remove: function(chat) { chats.splice(chats.indexof(chat), 1); }, get: function(chatid) { (var = 0; < chats.length; i++) { if (chats[i].id === parseint(chatid)) { return chats[i]; } } return null; } };
the chats list never populated. how correctly make ajax call inside ionic services.js file.
thank you!
you can this:
angular.module('starter.services', []) .factory('chats', function($http) { var api_url = 'https://api.herokuapp.com'; return { all: function() { return $http.get(api_url + '/chats'); } }; });
and in controller:
chats.all().then(function(chats){ $scope.chats = chats; })
you can check $http in docs
Comments
Post a Comment