在 ionic services.js 文件中对第三方 API 进行 ajax 调用
Making ajax calls to third party APIs in ionic services.js file?
今天开始使用离子,遇到了一个小颠簸。服务.js文件有注释 // Might use a resource here that returns a JSON array
我想对 API 进行 ajax 调用,但我不知道如何在该服务.js文件中解压缩自定义资源。我将从该调用中收到一个 JSON 数组,并将使用该列表。
问:如何将自定义变量声明为对第三个 pary API 进行 ajax 调用的列表?
angular.module('starter.services', [])
.factory('Chats', function() {
// Might use a resource here that returns a 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) {
for (var i = 0; i < chats.length; i++) {
if (chats[i].id === parseInt(chatId)) {
return chats[i];
}
}
return null;
}
};
聊天列表永远不会填充。如何在 ionic 服务.js文件中正确进行 ajax 调用。
谢谢!
你可以做这样的事情:
angular.module('starter.services', [])
.factory('Chats', function($http) {
var API_URL = 'https://api.herokuapp.com';
return {
all: function() {
return $http.get(API_URL + '/chats');
}
};
});
在控制器中:
Chats.all().then(function(chats){
$scope.chats = chats;
})
您可以在文档中查看$http
谢谢马蒂·图奇。我将在这里发布更详细的遮阳篷。
步骤:
-
在 app.js 文件内的视图中声明要使用的控制器。就我而言:
.state('tab.popular', { url: '/popular', views: { 'tab-popular': { templateUrl: 'templates/tab-popular.html', controller: 'PopularShowsCtrl' //<---CONTROLLER NAME YOU ARE USING } } })
-
在控制器.js文件中声明控制器。就我而言:
.controller('PopularShowsCtrl', function($scope, PopularShows){ $scope.popShows = PopularShows.all(); //<---$scope.popShows is a declaration and you don't need to declare this variable beforehand. })
-
现在你需要在你的服务.js文件中声明方法PopularShow.all((。在 ionic 中,你可以使用 .factory('YOUR_METHOD_NAME', function($http({...}( 来做到这一点。$http表示您的方法使用 http 请求。就我而言:
.factory('PopularShows', function($http){ // Might use a resource here that returns a JSON array return { all: function() { return $http.get('YOUR_API_URL'); }, get: function(popShowId) { // Simple index lookup return popShows[popShowId]; } } });
-
处理 popShow 变量,该变量是 html 文件中的 JSON 列表。就我而言:
<ion-view view-title="Popular"> <ion-content> <ion-list> <ion-item class="item-remove-animate item-avatar item-icon-right" ng-repeat="popShow in popShows" type="item-text-wrap"> <h2>{{popShow.name}}</h2> <p>{{popShow.popularity}}</p> <i class="icon ion-chevron-right icon-accessory"></i> <ion-option-button class="button-assertive" ng-click="remove(popShow)"> Delete </ion-option-button> </ion-item> </ion-list> </ion-content> </ion-view>
ng-repeat"popShow in popShow"将遍历列表。
结论:
如您所见,"PopularShow"是服务中的方法名称.js它是从控制器.js文件中调用的。PopularShow.all(( 进行 API 调用并将其返回给控制器。然后,控制器声明一个变量($scope.popShow(,该变量在html tab-popular中使用。
- 与运行长作业(javascript,node.js)的第三方API同步的最佳实践
- javascript api,用于在第三方域上存储身份验证令牌
- 与杜兰达尔合作的第一个JavaScript项目.尝试从第三方 API 获取数据
- 来自 react-redux-universal-hot-hot 示例初学者工具包中的第三方 API 的初始数据加载
- 从 Javascript 应用程序安全访问第三方 API
- 用于第三方 API 调用的 Web 应用体系结构
- 在 Firefoxaddonsdk 中使用第三方 API
- 在 ionic services.js 文件中对第三方 API 进行 ajax 调用
- JavaScript和扩展第三方API
- 使用第三方Oauth提供程序对API的用户进行身份验证
- 如何使用firebase从调用第三方API
- JavaScript-与第三方API的同步问题
- 使用带有第三方api的主干网
- 使用Node向第三方API发送GET请求
- 我可以使用Web API吗?播放按钮小部件的第三方Spotify移动应用程序
- 存储登录到第三方api的密码
- Javascript Android -绘制心电图不使用第三方API's
- $scope没有在第三方API异步调用后对视图应用更改
- 使用AngularJS从第三方API保存数据到CouchDB
- 第三方REST API基础认证