自定义 directive pagination
学习angular过程中,对directive 不是很了解,很有必要自己写一个,以便知道它的各方面的处理方式.
directive 中 scope 与 controller 交互,有三种定义策略 "=" ,"@" , "&" 另外一个就是 cope的 双向绑定.是要消耗时间的,并不能立即在controller 中使用 使用:
<mypagination start="start" length="length" recordstotal="recordsTotal" searchgrid="search()"></mypagination>
模版:
<div>
<ul class="pagination pull-left">
<li class="footable-page disabled"><div>显示 {{start+}} 到 {{start+length>recordstotal?recordstotal:start+length}} 项,共 {{recordstotal}} 项</div></li>
</ul>
<ul class="pagination pull-right">
<li ng-class="{disabled:!first}"><a ng-click="setpagination('first')">«</a></li>
<li ng-class="{disabled:!prev}"><a ng-click="setpagination('prev')">‹</a></li>
<li class="disabled"><a>{{ pageIndex }}</a></li>
<li ng-class="{disabled:!next}"><a ng-click="setpagination('next')">›</a></li>
<li ng-class="{disabled:!last}"><a ng-click="setpagination('last')">»</a></li>
</ul>
</div>
定义:
app.directive('mypagination', ['$rootScope', function ($rootScope) {
return {
restrict: 'E',
templateUrl: '/Scripts/App/tpls/mgitem/pagination.tpl.html',
replace: true,
transclude: true,
scope: {
start: '=',
length: '=',
recordstotal: '=',
searchgrid: '&'
},
controller: function ($scope, $element, $rootScope) {
var hassearch = false;
$scope.setpagination = function (pageflag) {
var newstart = ;
switch (pageflag) {
case "first":
newstart = ;
break;
case "prev":
newstart = ($scope.pageIndex - ) * $scope.length;
break;
case "next":
newstart = ($scope.pageIndex) * $scope.length;
break;
case "last":
newstart = ($scope.endIndex - ) * $scope.length;
break;
}
console.log('setpagination start=' + newstart);
$scope.start = newstart;
hassearch = true;
}
$scope.pagination = function () {
$scope.pageIndex = parseInt($scope.start / $scope.length) + ;
$scope.endIndex = parseInt($scope.recordstotal / $scope.length) + ;
$scope.first = $scope.pageIndex <= ? false : true;
$scope.last = $scope.pageIndex >= $scope.endIndex ? false : true;
$scope.prev = $scope.pageIndex > ? true : false;
$scope.next = $scope.pageIndex < $scope.endIndex ? true : false;
console.log('pagination recordstotal=' + $scope.recordstotal);
}
$scope.$watch('recordstotal', function () {
console.log('watch recordstotal');
$scope.pagination();
});
$scope.$watch('start', function () {
console.log('watch start');
if (hassearch)
{
$scope.searchgrid();
hassearch = false;
}
$scope.pagination();
});
},
compile: function(tElem, tAttrs){
//console.log(name + ': compile');
return {
pre: function(scope, iElem, iAttrs){
//console.log(scope.recordstotal + ': pre compile');
},
post: function(scope, iElem, iAttrs){
//console.log(scope.recordstotal + ': post compile');
}
}
},
link: function (scope, element, attris) {
scope.pageIndex = ;
scope.first = false;
scope.prev = false;
scope.next = false;
scope.last = false;
}
};
}])
自定义 directive pagination的更多相关文章
- 理解AngularJS生命周期:利用ng-repeat动态解析自定义directive
ng-repeat是AngularJS中一个非常重要和有意思的directive,常见的用法之一是将某种自定义directive和ng-repeat一起使用,循环地来渲染开发者所需要的组件.比如现在有 ...
- 关于angular 自定义directive
关于angular 自定义directive的小结 首先我们创建一个名为"expander"的自定义directive指令: angular.module("myApp& ...
- AngularJs中,如何在父元素中调用子元素为自定义Directive中定义的函数?
最近一段时间准备使用AngularJs中的自定义Directive重构一下代码. 在这里说明一下,把自定义控件封装成Directive并不一定是要复用,而是要让代码结构更加清晰.就好像你将一个长方法拆 ...
- AngularJS自定义Directive中link和controller的区别
在AngularJS中,自定义Directive过程中,有时用link和controller都能实现相同的功能.那么,两者有什么区别呢? 使用link函数的Directive 页面大致是: <b ...
- AngularJS自定义Directive不一定返回对象
AngularJS中,当需要自定义Directive时,通常返回一个对象,就像如下的写法: angular.module('modulename') .directive('myDirective', ...
- AngularJS自定义Directive初体验
通常我们这样定义个module并随之定义一个controller. var app = angular.module('myApp', []); app.controller('CustomersCo ...
- AngularJS自定义Directive
(编辑完这篇之后,发现本篇内容应该属于AngularJS的进阶,内容有点多,有几个例子偷懒直接用了官方的Demo稍加了一些注释,敬请见谅). 前面一篇介绍了各种常用的AngularJS内建的Direc ...
- AngularJs(Part 11)--自定义Directive
先对自定义Directive有一个大体的映像 myModule.directive('myDirective',function(injectables){ var directiveDefiniti ...
- 自定义Directive使用ngModel
我们知道ngModel是AngularJS中默认的一个Directive,用于数据的双向绑定.通常是这样使用的: <input type="text" ng-model=&q ...
随机推荐
- [MongoDB]MongoDB分页显示
MongoDB Limit与Skip方法配合进行分页MongoDB Limit() 方法如果你需要在MongoDB中读取指定数量的数据记录,可以使用MongoDB的Limit方法,limit()方法接 ...
- IntelliJ IDEA 2017.3尚硅谷-----设置项目文件编码
这也可以 R暂时显示 C转换
- 路飞-后台Django项目创建
后台:Django项目创建 环境 """ 为luffy项目创建一个虚拟环境 >: mkvirtualenv luffy """ &qu ...
- 鬼斧神工:求n维球的体积
原文地址:http://spaces.ac.cn/archives/3154/ 原文作者:苏剑林 标准思路 简单来说,\(n\)维球体积就是如下\(n\)重积分 \[V_n(r)=\int_{x_1^ ...
- [LEETCODE] 初级算法/数组 1.3旋转数组
原题: 给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: [1,2,3,4,5,6,7] 和 k = 3 输出: [5,6,7,1,2,3,4] 解释: 向右 ...
- Boxes and Candies
问题 G: Boxes and Candies 时间限制: 1 Sec 内存限制: 128 MB[提交] [状态] 题目描述 There are N boxes arranged in a row. ...
- 【Angular】关于angular引用第三方组件库无法改变其组件样式 :host ::ng-deep
[Angular]关于angular引用第三方组件库无法改变其组件样式 :host ::ng-deep css修改:无效 .ant-input-affix-wrapper .ant-input:not ...
- 【C语言】判断学生成绩等级
方法一:if-else #include<stdio.h> int main() { printf("请输入成绩:\n"); float score; scanf_s( ...
- linux sftp 和scp 运用
Linux scp命令: Upload to remote :复制本地文件到远程 Part1: Scp -P port local_file remote_user@remote_ip:rem ...
- acm数论之旅(转载) -- 逆元
ACM数论之旅6---数论倒数,又称逆元(我整个人都倒了( ̄﹏ ̄)) 数论倒数,又称逆元(因为我说习惯逆元了,下面我都说逆元) 数论中的倒数是有特别的意义滴 你以为a的倒数在数论中还是1/a吗 ( ...