[英]how to open authentification popup or modal in Angular 2
I am trying to code a login component for my application in Angular 2 version rc-1.我正在尝试在Angular 2版本 rc-1 中为我的应用程序编写登录组件。 But I did not find how to open my modal without a button action.
但是我没有找到如何在没有按钮操作的情况下打开我的模式。
When I load my app, I check if the user is logged or not (with the localStorage) : if he is not logged the login modal had to be openned.当我加载我的应用程序时,我检查用户是否已登录(使用 localStorage):如果他没有登录,则必须打开登录模式。
So open/close modal is conditionned by a boolean parameter and not a action button.所以打开/关闭模式由布尔参数而不是操作按钮决定。
Do you know how can I open a login modal without a button action ?您知道如何在没有按钮操作的情况下打开登录模式吗?
Thanks in advance,提前致谢,
Lets say you have a component that holds your modal.假设您有一个保存模态的组件。
export class YourModalComponent {
constructor () {}
public open(): void {
//show modal
}
}
And then you have a component that checks the local storage and has YourModalComponent
as a children.然后你有一个检查本地存储的组件,并将
YourModalComponent
作为子项。 If the user is logged in you just call modal.open()
.如果用户已登录,您只需调用
modal.open()
。
export class YourParentComponent {
@ViewChild(YourModalComponent)
public yourModal : YourModalComponent;
constructor() {}
public ngAfterViewInit(): void {
//check if user is logged in
if(!userIsLoggedIn) {
this.yourModal.open();
}
}
}
You can also have the div
displaying with ngIf
so the YourModalComponent
is only displayed if it meets the condition.您还可以使用
ngIf
显示div
,以便YourModalComponent
仅在满足条件时才显示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.