【问题标题】:Can ng-show directive be used with a delayng-show 指令可以延迟使用吗
【发布时间】:2015-01-08 14:19:58
【问题描述】:

我有一个微调器,显示为ng-show="loading>0"

有没有办法可以延迟显示这个微调器(比如 1 秒)?

我不能使用超时,因为有多个请求,加载计数器会不同步。

我需要的是通过 css 转换或类似方式延迟 ng-show

【问题讨论】:

  • 您是否已经查看过文档? link 滚动到底部,您将看到一个带有过渡和 ng-show 的示例
  • 您真正希望在这里完成什么?您想要延迟,以便对于快速操作它不只是 闪烁 打开和关闭,而是对于更长的操作它会显示?只是一个通用的加载指示器?

标签: angularjs angularjs-ng-show


【解决方案1】:

我怀疑您正在寻找一个包含延迟的通用微调器。标准,在200ms 或类似的东西之后显示。

这是指令的完美候选者,实际上很容易实现。

我知道这是一个很长的代码示例,但主要部分是指令。这很简单。

收听一些范围变量并在一些可配置的延迟后显示。如果操作花费的时间超过延迟,它将被取消并且永远不会出现。

(function() {
  'use strict';

  function SpinnerDirective($timeout) {
    return {
      restrict: 'E',
      template: '<i class="fa fa-cog fa-spin"></i>',
      scope: {
        show: '=',
        delay: '@'
      },
      link: function(scope, elem, attrs) {
        var showTimer;

        //This is where all the magic happens!
        // Whenever the scope variable updates we simply
        // show if it evaluates to 'true' and hide if 'false'
        scope.$watch('show', function(newVal){
          newVal ? showSpinner() : hideSpinner();
        });
        
        function showSpinner() {
          //If showing is already in progress just wait
          if (showTimer) return;

          //Set up a timeout based on our configured delay to show
          // the element (our spinner)
          showTimer = $timeout(showElement.bind(this, true), getDelay());
        }

        function hideSpinner() {
          //This is important. If the timer is in progress
          // we need to cancel it to ensure everything stays
          // in sync.
          if (showTimer) {
            $timeout.cancel(showTimer);
          }

          showTimer = null;

          showElement(false);
        }

        function showElement(show) {
          show ? elem.css({display:''}) : elem.css({display:'none'});
        }

        function getDelay() {
          var delay = parseInt(scope.delay);

          return angular.isNumber(delay) ? delay : 200;
        }
      }
    };
  }

  function FakeService($timeout) {
    var svc = this,
      numCalls = 0;

    svc.fakeCall = function(delay) {
      numCalls += 1;

      return $timeout(function() {

        return {
          callNumber: numCalls
        };

      }, delay || 50);
    };
  }

  function MainCtrl(fakeService) {
    var vm = this;

    vm.makeCall = function(delay) {
      vm.isBusy = true;
      fakeService.fakeCall(delay)
        .then(function(result) {
          vm.result = result;
        }).finally(function() {
          vm.isBusy = false;
        });
    }
  }

  angular.module('spinner', [])
    .service('fakeService', FakeService)
    .controller('mainCtrl', MainCtrl)
    .directive('spinner', SpinnerDirective);

}());
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" rel="stylesheet" />
<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular.min.js"></script>

<div class="container" ng-app="spinner">
  <div class="row" ng-controller="mainCtrl as ctrl">
    <div class="col-sm-12">
      <h2>{{ctrl.result | json}}
        <spinner show="ctrl.isBusy" delay="200"></spinner>
      </h2>
      <button type="button" 
              class="btn btn-primary" 
              ng-click="ctrl.makeCall(2000)" 
              ng-disabled="ctrl.isBusy">Slow Call
      </button>
      <button type="button" 
              class="btn btn-default" 
              ng-click="ctrl.makeCall()" 
              ng-disabled="ctrl.isBusy">Fast Call
      </button>
    </div>
  </div>
</div>

【讨论】:

  • 优秀的解决方案!
【解决方案2】:

这是一种更简单的方法,可以满足我的需求。根据您的操作,您可以将函数setDelay() 绑定到元素。例如,在我的例子中,我将setDelay() 绑定到一个选择输入。

触发 HTML:

<select class="first-option"
    ng-change="setDelay()" 
    ng-options="o.label for o in download.options" 
    ng-model="optionModel" required>
</select>

在您的控制器中,添加一个简单的函数setDelay,它将更改标志$scope.delay

$scope.setDelay = function(){
    $scope.delay = true;
    $timeout(function(){
        $scope.delay = false;
    }, 200);
};

然后,您可以在 ng-show 中简单地使用 $scope.delay 作为标志:

<div class="loading-div" ng-show="delay">
    <img src="loading_spinner.gif">
</div>

加载完成后显示内容:

<div ng-show="!delay">
    Content is loaded.
</div>

现在,每次用户在下拉菜单中选择一个新值时,都会触发$scope.delay 设置为true 导致显示微调器,当达到200 时,它将设置为false 导致微调器隐藏。

【讨论】:

    【解决方案3】:

    我认为纯 CSS 解决方案是最好的方法。

    这是一个plunker,展示了如何做到这一点。使用 ng-animate 类进行转换并应用 10ms 转换的转换延迟(0s 转换不适用于 css)。

    相关部分代码:

    .your-element-class.ng-hide {
      opacity: 0;
    }
    
    .your-element-class.ng-hide-add,
    .your-element-class.ng-hide-remove {
      transition: all linear 0.01s 1s;
    }
    

    为此使用自定义指令的唯一原因是在代码中使用大量不同延迟值的时间。自定义指令允许更多的延迟时间灵活性。

    【讨论】:

    • 这对我有帮助,并且是一种非常干净的方法,可以防止任何解决方法渗入 js 代码。我喜欢在 CSS 中保留尽可能多的样式/渲染逻辑。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-16
    • 2014-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-05
    相关资源
    最近更新 更多