zoukankan      html  css  js  c++  java
  • Angularjs判断页面是否已经渲染结束(动态给标签长度)

    相信大家都会碰到这样的问题。页面循环li。但是因为个数不知道。没有办法给li设置固定宽度。那么这时就需要动态计算数据长度并动态改变li的宽度

     1 <!--周边信息-->  
     2         <div class="around_information">  
     3             <div class="title_info">  
     4                 <i></i>  
     5                 <p>周边信息</p>  
     6             </div>  
     7             <div class="around_info">  
     8                 <div class="around_table">  
     9                     <ul>  
    10                         <li class="aroundLength" ng-repeat="data in aroundInfoData"on-finish-render-filters>  
    11                             <label>{{data.aroundName}}</label>  
    12                             <p>{{data.aroundNum}}</p>  
    13                         </li>  
    14                     </ul>  
    15                 </div>  
    16             </div>  
    17         </div> 

    <li></li>可以默认给个固定宽度,当然不给也是完全OK的

    控制器中的数据信息:

    $scope.aroundInfoData =[{"aroundName":"所在商圈", "aroundNum":"李宁商圈"},
        {"aroundName":"安踏", "aroundNum":"0"}, {"aroundName":"特步", "aroundNum":"1"},
        {"aroundName":"361°", "aroundNum":"0"},{"aroundName":"高中", "aroundNum":"3"},
        {"aroundName":"耐克", "aroundNum":"0"},{"aroundName":"阿迪达斯", "aroundNum":"2"},
        {"aroundName":"瑜伽馆", "aroundNum":"1"},{"aroundName":"大学", "aroundNum":"2"},
        {"aroundName":"篮球馆", "aroundNum":"0"},{"aroundName":"健身房", "aroundNum":"0"}]

    json数据可以自己造。想造多少都可以

    需要监听页面是否渲染结束。AngularJS有$last是最后一个元素。用到指令

    app.directive('onFinishRenderFilters', function ($timeout) {
        return {
            restrict: 'A',
            link: function(scope, element, attr) {
                if (scope.$last === true) {
                    $timeout(function() {
                        scope.$emit('ngRepeatFinished');
                    });
                }
            }
        };
    });

    Controller里面用$on去监听

    $scope.$on('ngRepeatFinished', function (ngRepeatFinishedEvent) {
            //监听页面渲染结束
            $scope.showLength =$scope.aroundInfoData.length  //把实际需要展示数据的格式赋给showLength
            var titleWidth = document.getElementsByClassName("aroundLength");
            for(var i =0;i<document.getElementsByClassName("aroundLength").length;i++){
                document.getElementsByClassName("aroundLength")[i].style.width = (100/$scope.showLength)+"%";
            }
        });

    预期结果:

  • 相关阅读:
    回家了
    AMP > Chapter 3 Concurrent Objects > Notes<1>
    Readings in Database Systems
    读书笔记:《Transaction Processing》Chapter 13 Buffer Management

    委托和事件的区别
    .net网页不完整的解决方案
    聚集索引,非聚集索引
    固定宽度下拉列表中option内容显示不全问题解决方法
    让你的Windows系统自动释放系统资源
  • 原文地址:https://www.cnblogs.com/Gherardo/p/7065062.html
Copyright © 2011-2022 走看看