gpt4 book ai didi

Angular 2 - 打开/关闭默认引导模式

转载 作者:太空狗 更新时间:2023-10-29 16:55:08 25 4
gpt4 key购买 nike

我不想使用 angular2-bootstrapng2-bs3-modal正如问题/答案中所建议的那样 Angular 2 Bootstrap ModalAngular 2.0 and Modal Dialog

现在。我知道什么打开和关闭引导模式。

  • 显示在 display: none;display: block;
  • 之间切换
  • div 上的一个属性在 aria-hidden="true"aria-hidden="false
  • 之间切换

所以,我很自然地认为,如果我像 [aria-hidden]="true" 这样绑定(bind)到 aria-hidden 属性,我就可以操纵它。遗憾的是,我无法绑定(bind)到 aria-hidden,因为它不是 div 的已知属性。 (注意,attr.aria-hidden 不存在)

我知道这可以通过带有 $('#myModal').modal() 的 JQuery 实现,如问题 How to open a Bootstrap modal window using jQuery? 所示。

所以我的问题是,是否有一个 TypeScript 功能与 JQuery 中的 modal() 做同样的事情,或者是否有一种方法可以将函数/变量绑定(bind)到 aria-hidden ?

我当前的 html:

<div id="createAccountModal" class="modal fade customForm" role="dialog" [aria-hidden]="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h4>Create account</h4>
</div>
<div class="modal-body">
<p>Lorem ipsum</P>
</div>
<div class="modal-footer align-left">
My custom footer
</div>
</div>
</div>
</div

最佳答案

您可以尝试这样的操作,创建 myModal.html:

<div class="modal-backdrop fade in" [style.display]="showModal ? 'block' : 'none'"></div>
<div class="modal" tabindex="-1" role="dialog" style="display: block" [style.display]="showModal ? 'block' : 'none'">
<div class="modal-dialog">
<div class="modal-popup">
<div class="popup-title">
<span>{{title}} </span>
<i class="icon-cancel fr" data-dismiss="modal" aria-label="Close" (click)="cancelAction()"></i>
<p *ngIf="subTitle">{{subTitle}}</p>
</div>
<ng-content></ng-content>
</div>
</div>
</div>

然后,创建myModal.component.ts:

import { Component, Input, Output, EventEmitter, OnInit } from '@angular/core';

const template: string = require('./myModal.html');

@Component({
selector: 'modal',
template
})

export class Modal implements OnInit {
@Input('show-modal') showModal: boolean;
@Input('title') title: string;
@Input('sub-title') subTitle: string;
@Input('cancel-label') cancelLabel: string;
@Input('positive-label') positiveLabel: string;

@Output('closed') closeEmitter: EventEmitter < ModalResult > = new EventEmitter < ModalResult > ();
@Output('loaded') loadedEmitter: EventEmitter < Modal > = new EventEmitter < Modal > ();
@Output() positiveLabelAction = new EventEmitter();

constructor() {}

ngOnInit() {
this.loadedEmitter.next(this);
}

show() {
this.showModal = true;
}

hide() {
this.showModal = false;
this.closeEmitter.next({
action: ModalAction.POSITIVE
});
}

positiveAction() {
this.positiveLabelAction.next(this);
return false;
}

cancelAction() {
this.showModal = false;
this.closeEmitter.next({
action: ModalAction.CANCEL
});
return false;
}
}

export enum ModalAction { POSITIVE, CANCEL }

export interface ModalResult {
action: ModalAction;
}

然后为此创建模块,这样你就可以在任何地方使用,像这样在任何地方使用它:

<modal #deleteUserModal id="deleteUser"
[show-modal]="isModalOpen"
[title]="'Delete'"
>
<div class="popup-content">
<p>Are you sure you want to delete the user permanently?</p>
</div>
<div class="popup-footer">
<button class="btn cancel" aria-label="Close" (click)="deleteUserModal.hide()">
Cancel
</button>
<button type="button" class="btn btn-primary" (click)="deleteSelectedUser(deleteUserModal)" aria-label="Close">
Delete
</button>
</div>
</modal>

你也可以增强它:)

关于Angular 2 - 打开/关闭默认引导模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41036737/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com