怎样才能实现在角度2中阻塞用户交互的块UI(http://malsup.com/jquery/block/#demos)?有人可以请帮忙。如何在角度2中实现块ui类型的行为
回答
spinner.ts
import {Component} from 'angular2/core';
@Component({
selector: 'spinner',
styleUrls: ['spinner.css'],
//I'm using in modal-backdrop classes from bootstrap
template:
`<div class="in modal-backdrop spinner-overlay"></div>
<div class="spinner-message-container" aria-live="assertive" aria-atomic="true">
<div class="spinner-message" [ngClass]="spinnerMessageClass">{{ state.message }}</div>
</div>`
})
export class SpinnerComponent {
state = {
message: 'Please wait...'
};
}
spinner.css
.spinner-overlay {
background-color: white;
cursor: wait;
}
.spinner-message-container {
position: absolute;
top: 35%;
left: 0;
right: 0;
height: 0;
text-align: center;
z-index: 10001;
cursor: wait;
}
.spinner-message {
display: inline-block;
text-align: left;
background-color: #333;
color: #f5f5f5;
padding: 20px;
border-radius: 4px;
font-size: 20px;
font-weight: bold;
filter: alpha(opacity=100);
}
spinner.service.ts
到您的组件import {Injectable, DynamicComponentLoader, ApplicationRef, ElementRef, ComponentRef} from 'angular2/core';
import {SpinnerComponent} from './spinner.component';
@Injectable()
export class SpinnerService {
spinnerComp: ComponentRef;
constructor(private componentLoader: DynamicComponentLoader, private appRef: ApplicationRef) { }
public start() {
let elementRef: ElementRef = this.appRef['_rootComponents'][0].location;
return this.startInside(elementRef, null);
}
public startInside(elementRef: ElementRef, anchorName: string) {
let spinnerRef = (!anchorName) ?
this.componentLoader.loadNextToLocation(SpinnerComponent, elementRef) :
this.componentLoader.loadIntoLocation(SpinnerComponent, elementRef, anchorName);
spinnerRef.then((compRef:ComponentRef) => {
this.spinnerComp = compRef;
});
}
public stop() {
if (this.spinnerComp) {
this.spinnerComp.dispose();
}
}
}
进样微调服务。调用开始和停止来显示和隐藏。
更新:演示plnkr链接:http://plnkr.co/edit/Y2ocRpbi2ORjbULrguDg
免责声明:我已经使用一个现有angular2库作为参考上面创建在我的项目代码。我正在寻找该图书馆,并在我找到该图书馆时在此处进行更新。
修改以前的答案,并更新Angular 2 RC1。
blockUi.ts(请注意,你需要一些软spinner.gif的)
import { Component, Input } from '@angular/core';
@Component({
selector: 'blockUi',
template:
`<div class="in modal-backdrop block-ui-overlay"></div>
<div class="block-ui-message-container" aria-live="assertive" aria-atomic="true">
<div class="block-ui-message">
<table>
<tr><td class='center space'>{{ message }}</td></tr>
<tr><td class='center'><img src="/styles/img/spinner.gif" /></td></tr>
</table>
</div>
</div>`,
styles: [`
.block-ui-overlay {
background-color: white;
cursor: wait;
}
.block-ui-message-container {
position: absolute;
top: 35%;
left: 0;
right: 0;
height: 0;
text-align: center;
z-index: 10001;
cursor: wait;
}
.block-ui-message {
display: inline-block;
text-align: left;
background-color: #333;
color: #f5f5f5;
padding: 20px;
border-radius: 4px;
font-size: 20px;
font-weight: bold;
filter: alpha(opacity=100);
}
.center {
text-align: center;
}
.space {
padding-bottom: 10px;
}`]
})
export class BlockUi {
message: string = 'Please wait...';
}
blockUiService.ts
import { Injectable, Inject, DynamicComponentLoader, ApplicationRef, ViewContainerRef, ComponentRef } from '@angular/core';
import { BlockUi } from '../components/blockUi';
@Injectable()
export class BlockUiService {
blockUi: ComponentRef<BlockUi>;
constructor(
@Inject(DynamicComponentLoader) private dynamicComponentLoader: DynamicComponentLoader,
@Inject(ApplicationRef) private applicationRef: ApplicationRef) { }
start(message?: string) {
const viewContainerRef: ViewContainerRef = this.applicationRef['_rootComponents'][0]._hostElement.component.viewContainerRef;
return this.startInside(viewContainerRef, message);
}
startInside(viewContainerRef: ViewContainerRef, message?: string) {
const blockUiRef = this.dynamicComponentLoader.loadNextToLocation(BlockUi, viewContainerRef);
blockUiRef.then((compRef: ComponentRef<BlockUi>) => {
this.blockUi = compRef;
if (message !== undefined && message !== null && message.length > 0) {
const blockUiInstance: BlockUi = compRef.instance;
blockUiInstance.message = message;
}
});
}
stop() {
if (this.blockUi) {
this.blockUi.destroy();
}
}
}
使用
constructor(
@Inject(BlockUiService) private blockUiService: BlockUiService
) {}
this.blockUiService.start("Saving Data...");
...
this.blockUiService.stop();
this.applicationRef ['_ rootComponents'] [0] ._ hostElement.component。viewContainerRef - > viewContainerRef未定义? (使用Angular 2 RC2) – Tomd
角2 blockui与微调(可根据您的需求定制)
请在下面找到与角2
spinner.component.ts
import {Component, Input, OnDestroy} from '@angular/core';
@Component({
selector: 'my-spinner',
//templateUrl: 'app/components/spinner/spinner.html'
template: `
<div [hidden]="!isDelayedRunning" class="spinnercontainer">
<div class="spinner">
<div class="double-bounce1"></div>
<div class="double-bounce2"></div>
</div>
</div>
`,
styleUrls:['app/spinner.component.css']
})
export class SpinnerComponent implements OnDestroy {
private currentTimeout: any;
private isDelayedRunning: boolean = false;
@Input()
public delay: number = 300;
@Input()
public set isRunning(value: boolean) {
if (!value) {
this.cancelTimeout();
this.isDelayedRunning = false;
}
if (this.currentTimeout) {
return;
}
this.currentTimeout = setTimeout(() => {
this.isDelayedRunning = value;
this.cancelTimeout();
}, this.delay);
}
private cancelTimeout(): void {
clearTimeout(this.currentTimeout);
this.currentTimeout = undefined;
}
ngOnDestroy(): any {
this.cancelTimeout();
}
}
spinner.component.css完美运行的代码
.spinnercontainer{
background-color: transparant;
height: 100vh;
width: 100%;
z-index: 20000;
position: fixed;
top: 0;
left: 0;
background-color: #000;
opacity: 0.5;
transition: all 0.5s ease;
display:block;
z-index: 20001;
}
.spinner {
width: 40px;
height: 40px;
position: relative;
margin: 100px auto;
}
.double-bounce1, .double-bounce2 {
width: 100%;
height: 100%;
border-radius: 50%;
background-color: #333;
opacity: 0.6;
position: absolute;
top: 0;
left: 0;
-webkit-animation: sk-bounce 2.0s infinite ease-in-out;
animation: sk-bounce 2.0s infinite ease-in-out;
}
.double-bounce2 {
-webkit-animation-delay: -1.0s;
animation-delay: -1.0s;
}
@-webkit-keyframes sk-bounce {
0%, 100% { -webkit-transform: scale(0.0) }
50% { -webkit-transform: scale(1.0) }
}
@keyframes sk-bounce {
0%, 100% {
transform: scale(0.0);
-webkit-transform: scale(0.0);
} 50% {
transform: scale(1.0);
-webkit-transform: scale(1.0);
}
}
使用方法: 在其他组件(使用异步任务)中使用上述组件,如下所示
样品view.component.ts
import {Component} from '@angular/core';
import {SpinnerComponent} from './spinner.component';
import {ApiService} from './apiService';
@Component({
selector: 'my-sample-view',
directives: [SpinnerComponent],
template: '<my-spinner [isRunning]="isRequesting"></my-spinner>',
})
export class SampleViewComponent {
public isRequesting: boolean;
public items: Array<any>;
constructor(private apiService: ApiService) {
this.refresh();
}
public refresh(): void {
this.isRequesting = true;
this.apiService.sampleHttpGetRequest()
.subscribe(
result => this.items = result,
() => this.stopRefreshing(),
() => this.stopRefreshing());
}
private stopRefreshing() {
this.isRequesting = false;
}
}
由于https://manuel-rauber.com/2016/01/05/angular-2-spinner-component/
- 1. 如何实现角度UI屏蔽?
- 2. 如何在角度http请求中实现client_credentials授予类型?
- 3. 如何实现这种类型的滚动角度材料sidenav?
- 4. 如何在角度2中减去日期类型的一年?
- 5. 如何在角度2中实现更改菜单
- 6. 实现在角2
- 7. 如何在jQuery或javascript中实现这种类型的行为
- 8. 如何在Objective C中实现mixin或trait类型的行为?
- 9. 如何实现HTTP在角2
- 10. 如何在角度2中导航到新的UI组件?
- 11. 如何为泛型类型实现equals?
- 12. 角度实现
- 13. 在GCC中实现的精确宽度整数类型如何?
- 14. 如何在Zend Framework 2中为搜索模块实现分页?
- 15. 渐变维度类型2的实现在SSIS
- 16. 如何实现角度加载更多的按钮2
- 17. 如何将角度UI Bootstrap注入打字稿中的角度模块
- 18. 如何在角度网络应用上实现角度游览
- 19. 角2 CheckboxControlValueAccessor实现
- 20. 角度2:如何创建对象,将角度翻译为角度2
- 21. 如何在.NET中实现值类型
- 22. 如何渲染类型HTMLImageElement的角2
- 23. 角度 - UI选择(选择2)高度
- 24. 角度安装模块2
- 25. 如何呈现在角2
- 26. 如何在PureScript中实现“finally tagless”类型类的Monadic实例?
- 27. 如何使用角度2来实现基于ACL /角色的授权?
- 28. 无法从角度2中的非角度模块导入
- 29. 将输入类型数字限制为2角度2中的小数位数
- 30. 如何使用角度为4的角度2包?
像你说的它的工作原理。谢谢 ! – deepugn
您能否发布一个关于如何在页面模板中使用这个示例的示例?谢谢。 – Dave
新增了plnkr连结。 @deepugn PLZ标记我的帖子为答案,如果这是你想要的。 –