zoukankan      html  css  js  c++  java
  • 自动刷新指令

    1.指令html模板

    <md-input-container class="wrap-auto-refresh">
        <label>{{'sscTimeRangePicker.refreshEvery' | translate}}</label>
        <md-select  style=" 100px" ng-model="intervalTime"  ng-change="changeTimeRefesh()" auto-id="refresh_interval_time">
            <md-option value="0"  auto-id="refresh_off"><span translate="sscTimeRangePicker.off">OFF</span></md-option>
            <md-option value="5" auto-id="refresh_5_secs">5 <span translate="sscTimeRangePicker.sec">Sec</span></md-option>
            <md-option value="15"  auto-id="refresh_15_secs">15 <span translate="sscTimeRangePicker.sec">Sec</span></md-option>
            <md-option value="20" auto-id="refresh_20_secs">20<span translate="sscTimeRangePicker.sec">Sec</span></md-option>
            <md-option value="30" auto-id="refresh_30_secs">30<span translate="sscTimeRangePicker.sec">Sec</span></md-option>
        </md-select>
    </md-input-container>

    2.指令内容 (主要操作为将该控件的的时间保存到本地localstoryeg
    (function(){
    
        "use strict";
    
    
        var nvsAutoRefresh   = function  (){
            return{
    
                controller:function($scope,$interval,$timeout,localStorageService){
                    //auto refesh
                    var   autoRefresh;
                    (function(){
                        if(!localStorageService.get("autoRefreshTime")){
                            $scope.intervalTime = 15;
                            localStorageService.set("autoRefreshTime",$scope.intervalTime);
                            console.log(localStorageService.get("autoRefreshTime"));
                        }else{
                            $scope.intervalTime =  localStorageService.get("autoRefreshTime");
                            console.log(localStorageService.get("autoRefreshTime"));
                        }
    
                    })();
                    $scope.changeTimeRefesh = function(){
                        var intervalValue = $scope.intervalTime;
                        console.log("间隔间隔intervalValue"+intervalValue);
    
                        if(intervalValue == "0"){
                            if(!localStorageService.get("autoRefreshTime")){
                            console.log("定时刷新已关闭");
                            }else{
                                localStorageService.set("autoRefreshTime",intervalValue);
                            }
                            }else {
                                localStorageService.set("autoRefreshTime",intervalValue);
                            }
    
                        }
                        
                    // $scope.$watch(function () {
                    //     return $state.current.name;
                    //     console.log($state.current.name);
                    // },function(newVal, oldVal){
                    //     console.log("lalalal" + newVal,oldVal);
                    //     if( newVal =="activeAlarms"|| newVal =="historicalAlarms"){
                    //         $interval.cancel(autoRefresh);
                    //
                    //         $scope.refreshPage();
                    //
                    //     }else{
                    //         $interval.cancel(autoRefresh);
                    //     }
                    //
                    // });
    
                },
                restrict:'E',
                templateUrl: 'src/common/directive/nvs-auto-refresh/nvs-auto-refresh.html'
    
            };
    
        }
        angular.module('nvs-auto-refresh',[])
            .directive('nvsAutoRefresh',nvsAutoRefresh);
    
    })();

     3.在用到该控件的的地方  写入下方法

     var autoFresh = function () {
                    var timeRange = Number(localStorageService.get("autoRefreshTime"));
                    console.log("timeRange   eeeeeeeeeee"+timeRange);
                    var autoRefresh;
                    if(timeRange > 0){
                        autoRefresh = $interval(function() {
                            var timeDistance = $scope.toDate - $scope.fromDate;
                            $scope.tDateRange = angular.copy(new Date());
                            $scope.fDateRange = angular.copy(new Date($scope.tDateRange.getTime() - timeDistance));
                            $scope.fromDate = $scope.fDateRange.getTime();
                            $scope.toDate = $scope.tDateRange.getTime();
    
                            $timeout(function() {
                                $scope.refreshPage();
                            })
                        }, timeRange * 1000);
                    }
    
    
                    $scope.$on("$destroy", function() {    //离开页面时相关操作
                        $interval.cancel(autoRefresh);
                    })
    
                };
    
                autoFresh();

    指令使用需要在index引入指令的js文件  在总moudel加入依赖

  • 相关阅读:
    git 之gitignore 添加项之后生效的问题
    使用 padding-bottom 设置高度基于宽度的自适应
    ES5中新增的Array方法详细说明
    zepto.js 自定义打包集成其他模块构建流程
    移动端如何让页面强制横屏
    快来看看抓包工具有哪些?
    实践出真知,小程序wepy,uni-app框架开发使用!
    开发过程遇到的css样式问题记录
    带坑使用微信小程序框架WePY组件化开发项目,附带第三方插件使用坑
    微信 + weui 框架记录
  • 原文地址:https://www.cnblogs.com/RonnieQin/p/9168401.html
Copyright © 2011-2022 走看看