博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
自定义 directive pagination
阅读量:4697 次
发布时间:2019-06-09

本文共 3505 字,大约阅读时间需要 11 分钟。

 学习angular过程中,对directive 不是很了解,很有必要自己写一个,以便知道它的各方面的处理方式.

directive 中 scope 与 controller 交互,有三种定义策略 "=" ,"@" , "&" 另外一个就是 cope的 双向绑定.是要消耗时间的,并不能立即在controller 中使用 使用:

模版:

  • 显示 {
    {start+1}} 到 {
    {start+length>recordstotal?recordstotal:start+length}} 项,共 {
    {recordstotal}} 项

定义:

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 = 0;                switch (pageflag) {                    case "first":                        newstart = 0;                        break;                    case "prev":                        newstart = ($scope.pageIndex - 2) * $scope.length;                        break;                    case "next":                        newstart = ($scope.pageIndex) * $scope.length;                        break;                    case "last":                        newstart = ($scope.endIndex - 1) * $scope.length;                        break;                }                console.log('setpagination start=' + newstart);                $scope.start = newstart;                hassearch = true;                           }            $scope.pagination = function () {                $scope.pageIndex = parseInt($scope.start / $scope.length) + 1;                $scope.endIndex = parseInt($scope.recordstotal / $scope.length) + 1;                $scope.first = $scope.pageIndex <= 1 ? false : true;                $scope.last = $scope.pageIndex >= $scope.endIndex ? false : true;                $scope.prev = $scope.pageIndex > 1 ? 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 = 1;            scope.first = false;            scope.prev = false;            scope.next = false;            scope.last = false;                    }    };}])

 

转载于:https://www.cnblogs.com/CoreXin/p/5692244.html

你可能感兴趣的文章
Unity多开的方法
查看>>
File类中的list()和listFiles()方法
查看>>
我的VS CODE插件配置 主要针对.NET和前端插件配置
查看>>
关于js中的事件
查看>>
一致性哈希算法运用到分布式
查看>>
决策树和随机森林->信息熵和条件熵
查看>>
iOS10 UI教程视图和子视图的可见性
查看>>
Maven学习笔记
查看>>
FindChildControl与FindComponent
查看>>
1、简述在java网络编程中,服务端程序与客户端程序的具体开发步骤?
查看>>
C# Web版报表
查看>>
中国城市json
查看>>
android下载手动下载Android SDK
查看>>
C++学习:任意合法状态下汉诺塔的移动(原创)
查看>>
学霸修炼的秘籍
查看>>
Duplicate 复制数据库 搭建Dataguard
查看>>
leetcode133 - Clone Graph - medium
查看>>
Mybatis(一)入门
查看>>
DDR工作原理(转)
查看>>
(Frontend Newbie) Web三要素(一)
查看>>