学习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; }
};
}])

最新文章

  1. ecshop不同样式文章页调用不同文章模板
  2. ListView下拉刷新
  3. 基于NodeJS的全栈式开发
  4. GPRS GPRS(General Packet Radio Service)是通用分组无线服务技术的简称,它是GSM移动电话用户可用的一种移动数据业务,属于第二代移动通信中的数据传输技术
  5. cpp
  6. 【转】ubuntu中的Wine详解
  7. 【虚拟化实战】容灾设计之四VPLEX
  8. Laravel 使用简述
  9. 【持久化框架】Mybatis简介与原理
  10. 201521123067 《Java程序设计》第6周学习总结
  11. Concurrent包常用方法简介
  12. day 24 二十四、组合、继承、方法重写和重用、super()
  13. Win10安装LoadRunner11
  14. poj3126 Prime Path(c语言)
  15. 【转】 H.264编码原理以及I帧B帧P帧
  16. App 添加权限
  17. IOI2018题解
  18. 利用Py-Socket模块做的一个不登陆windows服务器自动实现替换或者调用自动拨号功能
  19. BZOJ4650: [Noi2016]优秀的拆分(hash 调和级数)
  20. Session丢失——解决方案

热门文章

  1. MyBatis(6)——分页的实现
  2. 最详细的linux安装php过程
  3. dw选择器
  4. Codeforces Round #598 (Div. 3) F. Equalizing Two Strings
  5. KM poj 2195
  6. python的scribe client
  7. 启动MySQL5.7时报错:initialize specified but the data directory has files in it. Aborting.
  8. element-ui表头render-header 传自定义参数
  9. 吴裕雄--天生自然神经网络与深度学习实战Python+Keras+TensorFlow:使用TensorFlow和Keras开发高级自然语言处理系统——LSTM网络原理以及使用LSTM实现人机问答系统
  10. C语言:输入一个数,输出比这个数小的所有素数,并求出个数。