在Angular中,为了使用DOM,您需要一个指令。而要控制装载机,你需要一个服务。 Here is a very simple demo for you。其原理是:指令手表服务状态变化,并绘制 “装载机” 如果服务说,装载机768,16呈现:
的index.html
<!DOCTYPE html>
<html data-ng-app="Demo">
<head>
<script data-require="[email protected]" data-semver="1.2.22" src="https://code.angularjs.org/1.2.22/angular.js"></script>
<link rel="stylesheet" href="style.css" />
<script src="script.js"></script>
</head>
<body data-ng-controller="LoaderController as ctrl">
<div data-loader class="loader" data-ng-class="{'visible':Loader.visible}"></div>
<button data-ng-click="ctrl.show(true)">Loader</button>
</body>
</html>
的script.js
(function() {
'use strict';
angular.module('Demo', []);
angular.module('Demo').directive('loader', [function(){
return {
'restrict' : 'A',
'controller' : ['$scope', 'Loader', function($scope, Loader){
$scope.Loader = Loader;
}]
}
}]);
angular.module('Demo').factory('Loader', [function(){
var instance = {}
instance.show = function(on){
instance.visible = on;
}
return instance;
}]);
angular.module('Demo').controller('LoaderController', ['$timeout', 'Loader', function($timeout, Loader){
this.show = function(){
Loader.show(true);
$timeout(function(){
Loader.show(false);
}, 5000)
}
}]);
})();
style.css
html, body {
margin: 0;
padding: 0;
}
.loader {
opacity: .5;
position: absolute;
width: 100%;
height: 100%;
background-color: rgba(255, 0, 0, 0.5);
display: none;
}
.loader.visible {
display: block;
}
你需要玩Z-index的绝对/固定位置。你有演示..? – PSL 2014-09-22 21:21:13
看看[角加载栏](http://chieffancypants.github.io/angular-loading-bar/) – meriadec 2014-09-22 21:25:30