ng-show指令可以延迟使用吗
Can ng-show directive be used with a delay
我有一个微调器,显示为 ng-show="loading>0"
有什么方法可以延迟显示此微调器(比如 1 秒)?
我不能使用超时,因为有多个请求,卸载计数器将不同步。
我需要的是通过 css 转换或类似
延迟 ng-show
我怀疑 您正在寻找包含延迟的通用微调器。标准,显示在 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>
这是一种更简单的方法,可以满足我的需要。根据您的操作,您可以将函数 setDelay()
绑定到元素。例如,在我的例子中,我将 setDelay()
绑定到 select 输入。
触发器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);
};
然后,您可以简单地使用 $scope.delay
作为 ng-show 中的标志:
<div class="loading-div" ng-show="delay">
<img src="loading_spinner.gif">
</div>
加载完成后显示内容:
<div ng-show="!delay">
Content is loaded.
</div>
现在,每次用户 select 在下拉菜单中输入新值时,都会触发 $scope.delay
设置为 true
导致微调器显示,当它达到 200
,它将被设置为 false
,导致微调器隐藏。
我认为纯粹的 CSS 解决方案是最好的方法。
这里 plunker 显示了如何操作。使用 ng-animate 类 进行过渡并应用过渡延迟 10 毫秒的过渡(0 秒过渡不适用于 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;
}
为其使用自定义指令的唯一原因是在您的代码中使用不同延迟值的大量时间。自定义指令允许更灵活的延迟时间。
我有一个微调器,显示为 ng-show="loading>0"
有什么方法可以延迟显示此微调器(比如 1 秒)?
我不能使用超时,因为有多个请求,卸载计数器将不同步。
我需要的是通过 css 转换或类似
延迟ng-show
我怀疑 您正在寻找包含延迟的通用微调器。标准,显示在 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>
这是一种更简单的方法,可以满足我的需要。根据您的操作,您可以将函数 setDelay()
绑定到元素。例如,在我的例子中,我将 setDelay()
绑定到 select 输入。
触发器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);
};
然后,您可以简单地使用 $scope.delay
作为 ng-show 中的标志:
<div class="loading-div" ng-show="delay">
<img src="loading_spinner.gif">
</div>
加载完成后显示内容:
<div ng-show="!delay">
Content is loaded.
</div>
现在,每次用户 select 在下拉菜单中输入新值时,都会触发 $scope.delay
设置为 true
导致微调器显示,当它达到 200
,它将被设置为 false
,导致微调器隐藏。
我认为纯粹的 CSS 解决方案是最好的方法。
这里 plunker 显示了如何操作。使用 ng-animate 类 进行过渡并应用过渡延迟 10 毫秒的过渡(0 秒过渡不适用于 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;
}
为其使用自定义指令的唯一原因是在您的代码中使用不同延迟值的大量时间。自定义指令允许更灵活的延迟时间。