未学者(wo)一步一步从ionic2学习到AngularJs(三)
发布时间:2020-12-17 09:05:03 所属栏目:安全 来源:网络整理
导读:通过Service层获取数据列表 上一次中页面的基本架构已完成,这一次将尝试如何通过service层从服务器请求数据,在通过controller层为载体,显示到视图层。 在services.js的Tab1Service中添加一个getList的函数,用来获取数据列表 .service('Tab1Service',func
|
通过Service层获取数据列表 上一次中页面的基本架构已完成,这一次将尝试如何通过service层从服务器请求数据,在通过controller层为载体,显示到视图层。
.service('Tab1Service',function ($http) {
this.getClassify = function () {
return [
{ name: '健康资讯',viewable: true,url: domain + '/info/list',page: 1,rows: 20 },{ name: '健康知识',viewable: false,url: domain + '/lore/list',{ name: '健康问答',url: domain + '/ask/list',{ name: '健康图书',url: domain + '/book/list',rows: 20 }
]
}
this.getList = function (url,page,rows) {
return $http.post(url,{ page: page,rows: rows })
}
})
angular.module('starter.controllers',[])
.controller('Tab1Ctrl',function ($scope,$rootScope,Tab1Service,$ionicSlideBoxDelegate,$ionicTabsDelegate) {
$rootScope.imgUrl = imgUrl;
var classify = Tab1Service.getClassify()
$scope.slides = classify;
$scope.tabs = classify;
var slideIndex = 0;
Tab1Service.getList(classify[0].url,1,20).then(function (response) {
if (response.data.status) {
$scope.items = response.data.tngou;
console.log(response.data);
}
},function (error) {
console.log(error);
})
$scope.slideChanged = function (index) {
//这里使用instances[1]的原因是视图中有两个tabs
$ionicTabsDelegate._instances[1].select(index);
};
$scope.$on('$ionicView.afterEnter',function () {
//等待视图加载完成的时候默认选中第一个菜单
$ionicTabsDelegate._instances[1].select($ionicSlideBoxDelegate.currentIndex());
});
$scope.selectedTab = function (index) {
//滑动的索引和速度
$ionicSlideBoxDelegate.slide(index)
}
})
.controller('Tab2Ctrl',function($scope) {})
.controller('Tab3Ctrl',function($scope) {})
.controller('Tab4Ctrl',function($scope) {})
.controller('AccountCtrl',function($scope) {});
<ion-view view-title="健康">
<ion-content class="has-header">
<ion-slide-box show-pager="false" class="has-header" on-slide-changed="slideChanged($index)">
<ion-slide ng-repeat="slide in slides">
<div class="list">
<a ng-repeat="item in items" class="item item-thumbnail-right item-text-wrap" href="#">
<img ng-src="{{imgUrl+item.img}}" width="30" height="30" alt="">
<h3>{{::item.title}}</h3>
<p>{{::item.description | substring:item.description}}</p>
</a>
</div>
</ion-slide>
</ion-slide-box>
</ion-content>
<ion-tabs class="tabs-striped tabs-top">
<ion-tab ng-repeat="item in tabs" on-select="selectedTab($index)" title="{{item.name}}"></ion-tab>
</ion-tabs>
</ion-view>
代码解释 视图层只有list内的标签做了改动。
.service('BaseService',function ($http) {
this.loadMore = function ($this) {
console.log("正在加载更多数据..." + $this.page);
$http.jsonp($this.url + "?page=" + $this.page + "&rows=" + settings.rows + "&callback=JSON_CALLBACK").success(function (response) {
console.log(response);
if (response.tngou.length > 0) {
$this.items = $this.items.concat(response.tngou);
$this.page++;
} else {
console.log("没有数据了...")
$this.isload = true;
}
$this.callback();
});
}
this.doRefresh = function ($this) {
console.log("正在执行refresh操作...");
//使用jsonp的方式请求
$http.jsonp($this.url + "?page=1&rows=" + settings.rows + "&callback=JSON_CALLBACK").success(function (response) {
console.log(response);
$this.page = 2;
$this.items = response.tngou;
$this.callback();
$this.isload = false;
});
}
})
效果图: 相关标签:AndroidAngularJSWebApp 作者: 栩栩然 链接:http://www.imooc.com/article/15995?block_id=tuijian_wz 来源:慕课网 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
