今天来跟大家分享一个小的demo,一般网页浏览到底部的时候会有一个点击加载更多的按钮,之前一直纠结怎么写这个,今天学习angular时发现可以用组件来实现这么一个小的效果,大家有兴趣的话可以看一下。

点击加载更多,代码如下:

<!DOCTYPE html>
<html ng-app="indexApp">//绑定模块
<head>
<meta charset="UTF-8">
<title></title>
<style>
#fixed{
height: 500px;
overflow: auto;
}
</style>
<script src="../js/angular.1.5.6.js"></script>//引入angular库
<script>
var app = angular.module('indexApp',[]);//定义模块
app.controller('indexCtrl',['$scope',function($scope){//定义控制器
$scope.items = ['a','b'];
for(var i=0;i<=30;i++){
$scope.items.push(Math.random()*10);
}
$scope.loca = function(){
for(var j=0;j<=10;j++){
$scope.items.push(Math.random()*10);
}
} }]);
app.directive('ngScroll',function(){//组件
return {
template:'<ul><li ng-repeat="item in items">{{item}}</li></ul>',
}
});
</script>
</head>
<body>
<div ng-controller="indexCtrl">
<div id="fixed" ng-scroll=""></div><!--指令的方式-->
<button ng-click="loca()">查看更多</button>
</div>
</body>
</html> 滑动到底部自动加载:
<!DOCTYPE html>
<html ng-app="loadMoreApp">
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <style>
        #fixed {
            height: 500px;
            overflow: auto;/*溢出显示滚动条*/
        }
    </style>
    <body>
        <div ng-controller="indexCtrl">
            <div id="fixed" ng-scroll="loadMore()"><!--指令的方式-->
                
            </div>
        </div>
    </body>
    <script src="js/angular.js"></script>
    <script>
        var app = angular.module('loadMoreApp', []);//定义一个模块
        app.controller('indexCtrl', ['$scope', function($scope) {//定义一个控制器
            $scope.items = ['a', 'b'];
            var i = 0;
            for(; i <= 30; i++) {
                $scope.items.push(Math.random() * 10);//生成随机数,加到items这个数组后
            }
            $scope.loadMore = function() {
                var j = 0;
                for(; j <= 10; j++) {
                    $scope.items.push(Math.random() * 10);
                }
            }
            
        }]);
        //定义一个组件
        app.directive('ngScroll', [function() {
            return {
                template: '<ul><li ng-repeat="item in items">{{item}}</li></ul>',
                link: function(scope, ele, attr) {
                    //                    console.log(ele);
                    ele.bind('scroll', function(e) {
//                        console.log("offsetHeight:" + e.target.offsetHeight)
//                        console.log("scrollTop:" + e.target.scrollTop) //滚动的距离
//                        console.log("scrollHeight" + e.target.scrollHeight)
                        if(e.target.offsetHeight + e.target.scrollTop >= e.target.scrollHeight) {
                            console.log("你已经到底了")
                            scope.$apply(attr.ngScroll);
                        }
                    })
                }
            }
        }])
    </script>
</html>

angular点击查看更多(简单demo)的更多相关文章

  1. 利用ScrollView滑动属性实现点击查看更多

    利用ScrollView的滚动实现点击查看更多 效果图 更新内容布局 <ScrollView android:id="@+id/sv_des" android:layout_ ...

  2. JS点击查看更多内容 控制段落文字展开折叠

    JavaScript+jQuery实现的文字展开折叠效果,点击文字后文字内容会完整的显示出来,控制段落来显示文字,不需要的时候,可以再次点击后将内容折叠起来,也就是隐藏了一部分内容.点击查看更多的功能 ...

  3. jquery 点击查看更多箭头变化,文字变化,超出带滚动条。

    从网上好了好久,没找到自己要的,自己写了一下. <!DOCTYPE html> <html> <head> <meta charset="utf-8 ...

  4. jQ-点击查看更多

    <style type="text/css"> .hi { width: 200px; height: 18vw; background-color: pink; fo ...

  5. angular实现了一个简单demo,angular-weibo-favorites

    前面必须说一段 帮客户做了一个过渡期的项目,唯一的要求就是速度,我只是会点儿基础的php,于是就用tp帮客户做了这个项目.最近和客户架构沟通,后期想把项目重新做一下,就用现在最流行的技术,暂时想的使用 ...

  6. Deferred在jQuery和Angular中的使用与简单实现

    Deferred在jQuery和Angular中的使用与简单实现 Deferred是在jQuery1.5版本中加入的,并且jQuery使用它完全重写了AJax,以前也只是偶尔使用.但是上次在使用Ang ...

  7. iOS中"查看更多/收起"功能实现

    实现效果如图: 查看更多功能在很多app种都有应用,在这里简单的实现,介绍实现流程: 一个tableViewCell中包含一个collectionView,"查看更多"按钮是tab ...

  8. Spring的简单demo

    ---------------------------------------- 开发一个Spring的简单Demo,具体的步骤如下: 1.构造一个maven项目 2.在maven项目的pom.xml ...

  9. jquery 点击显示更多

    点击显示更多 html <div class="servicepicture banxin"> <div class="imgcontent" ...

随机推荐

  1. 使用 nodeJs 开发微信公众号(配置服务器)

    流程如下: 1. 申请微信公众号:企业号.服务号.订阅号(前两个要钱) 2. 配置微信公众号后台 选择基本配置,获得 AppId 和 AppSecret ,点击服务器配置 URL:你服务器地址,不能是 ...

  2. Xilinx Zynq ZC-702 开发(02)—— 通过 Xilinx SDK 调试 Linux 应用

    远程调试环境由 PC 上运行的 System Debugger(集成在 Xilinx SDK 中) 和 Zynq 板上运行的 Linux TCF Agent 共同构成, 两者通过 TCP 连接,架构图 ...

  3. window Linux 双系统安装

    我是先安装的win10,然后在其基础上又安装了Ubuntu 16.04,为了今后再次安装方便,这里记录一下安装过程. 我在安装时主要参考了文章:https://blog.csdn.net/flyyuf ...

  4. JS中this的四种用法

    1.在一般函数方法中使用 this 指代全局对象 2.作为对象方法调用,this 指代上级对象 3.作为构造函数调用,this 指代new 出的对象 4.apply 调用 ,apply方法作用是改变函 ...

  5. python 二叉树实现带括号的四则运算

    #!/usr/bin/python #* encoding=utf-8 s = "20-5*(0+1)*5^(6-2^2)" c = 0 top = [0,s[c],0] op = ...

  6. lucene复合条件查询案例——查询name域 或 description域 包含lucene关键字的两种方式

    方式一:使用语法表达式查询 //查询name域 或 description域包含lucene关键字 QueryParser queryParser = new QueryParser("na ...

  7. 怎样用Python的Scikit-Learn库实现线性回归?

    来源商业新知号网,原标题:用Python的Scikit-Learn库实现线性回归 回归和分类是两种 监督 机器 学习算法, 前者预测连续值输出,而后者预测离散输出. 例如,用美元预测房屋的价格是回归问 ...

  8. CHROME浏览器清缓存

  9. 红黑树(red-black tree)实现记录

    https://github.com/xieqing/red-black-tree A Red-black Tree Implementation In C There are several cho ...

  10. try-catch-finally 与返回值的修改

    先看一段java代码,func返回值为int: public static int func() { int result = 0; try { result = 1; return result; ...