我正在關注modal dialog的解決方案以及下面的註釋。由於他們是這麼多我決定爲清晰起見,我有改編版本的新問題:Angular2:Modal對話框沒有捕捉到關鍵事件
import { Component, ViewChild } from '@angular/core';
import { ModalDirective } from 'ngx-bootstrap';
@Component({
selector: 'app-modal',
template: `
<div #myModal class="modal fade" tabindex="-1" [ngClass]="{'in': visibleAnimate}"
[ngStyle]="{'display': visible ? 'block' : 'none', 'opacity': visibleAnimate ? 1 : 0}">
<div class="modal-dialog">
<div class="modal-content" (click)="$event.stopPropagation();">
<div class="modal-header">
<ng-content select=".app-modal-header"></ng-content>
</div>
<div class="modal-body">
<ng-content select=".app-modal-body"></ng-content>
</div>
<div class="modal-footer">
<ng-content select=".app-modal-footer"></ng-content>
</div>
</div>
</div>
</div>
`
})
export class ModalComponent {
@ViewChild('myModal') public myModal: ModalDirective;
public visible = false;
private visibleAnimate = false;
public show(): void {
this.visible = true;
setTimeout(() => this.visibleAnimate = true);
document.body.className += ' modal-open';
}
public hide(): void {
this.visibleAnimate = false;
document.body.className = document.body.className.replace('modal-open', '');
setTimeout(() => this.visible = false, 300);
}
public onContainerClicked(event: MouseEvent): void {
if ((<HTMLElement>event.target).classList.contains('modal')) {
this.hide();
}
}
}
我現在有這個方法的主要問題是:
- 鍵盤事件沒有在模態對話框中被捕獲,而是被髮送到後向。如何防止這一點?
- 如評論部分所述:如何多次覆蓋對話框?即爲了例如具有模態對話。編輯而不是額外的錯誤通知。這似乎在後臺...
編輯: 檢查幾個來源後,我想通了以下內容:
我需要安裝ngx-bootstrap
,添加在app.module.ts
import { ModalModule } from 'ngx-bootstrap';
...
@NgModule({
imports: [
...
ModalModule
],
並加入systemjs.config.js
// ngx-bootstrap
'ngx-bootstrap' : {
format : 'cjs',
main : 'bundles/ngx-bootstrap.umd.js',
defaultExtension : 'js'
},
'moment' : {
main : 'moment.js',
defaultExtension : 'js'
},
隨着上述變化(和udpated代碼)我仍然有問題得到第一個前面的第二個模態對話框。提示?
對不起,遲到的答覆。我現在測試它,我不確定整個樣本應該是什麼樣子。 'ModalDirective'將從'ngx-bootstap'導入。在app.module.ts中,我導入了'ModalDirective'(需要ModalModule?),並試圖關注你的鏈接http://stackoverflow.com/a/42633720/325868。但不知何故鉻加載'ngx-bootstrap'有問題 - 即我得到一個'錯誤 - 意外的令牌<... zone.js:365 ....評估http://..../ngx-bootstrap。 ..' - 任何想法? – LeO
我想出瞭如何解決我最後的問題 - 但問題仍然存在 - 見上面 – LeO
我玩過,並發現可以採取最初的方法。儘管如此,額外的安裝是學習依賴關係的良好實踐;-) – LeO