bro*_*rsa 9 angularjs angularjs-ng-show
我有一个旋转器显示 ng-show="loading>0"
有没有办法可以延迟显示这个微调器(比如1秒)?
我不能使用超时,因为多个请求加载计数器将不同步.
我需要的是ng-show
通过css转换或类似的延迟
我怀疑你正在寻找一个包含延迟的通用微调器.标准,显示后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);
}());
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)
小智 5
这是一种可以满足我的需求的简单方法。根据您的操作,将功能绑定setDelay()
到元素。例如,就我而言,我绑定setDelay()
到选择输入。
触发HTML:
<select class="first-option"
ng-change="setDelay()"
ng-options="o.label for o in download.options"
ng-model="optionModel" required>
</select>
Run Code Online (Sandbox Code Playgroud)
在您的控制器中,添加一个简单的函数setDelay
来更改标志$scope.delay
:
$scope.setDelay = function(){
$scope.delay = true;
$timeout(function(){
$scope.delay = false;
}, 200);
};
Run Code Online (Sandbox Code Playgroud)
然后,您可以$scope.delay
在ng-show中简单地将其用作标志:
<div class="loading-div" ng-show="delay">
<img src="loading_spinner.gif">
</div>
Run Code Online (Sandbox Code Playgroud)
并在加载完成后显示内容:
<div ng-show="!delay">
Content is loaded.
</div>
Run Code Online (Sandbox Code Playgroud)
现在,每次用户在下拉菜单中选择一个新值时,都会触发$scope.delay
将其设置为true
导致微调框显示,而当到达时200
,它将被设置为false
导致微调框隐藏。
归档时间: |
|
查看次数: |
13569 次 |
最近记录: |