ng-bootstrapからモーダルポップアップを表示するには、次のコードを使用します。この例では、ボタンがクリックされると、open(content)
というファンクションがトリガされてビューが表示されます。ご覧のとおり、#content
がHTMLテンプレートで定義されています。しかし、これは私が欲しいものではありません。私はボタンを完全に取り除き、このポップアップをプログラム的にトリガーしたい。したがって、私は何とか私のコンポーネントの#content
への参照を取得する必要があります。Angular 2 TypescriptのコンポーネントからHTML内の変数へのアクセスを取得
<template #content let-c="close" let-d="dismiss">
<div class="modal-header">
<h4 class="modal-title">Modal title</h4>
<button type="button" class="close" aria-label="Close" (click)="d('Cross click')">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p>One fine body…</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" (click)="c('YES')">YES</button>
<button type="button" class="btn btn-secondary" (click)="c('NO')">NO</button>
</div>
</template>
<!--I dont want this button to trigger the template in my actual app. So imagine this code is not here!-->
<button class="btn btn-lg btn-outline-primary" (click)="open(content)">Launch demo modal</button>
コンポーネントでopen(content)
のコード:
open(content) {
this.modalService.open(content).result.then((result) => {
this.closeResult = `Closed with: ${result}`;
}, (reason) => {
this.closeResult = `Dismissed ${this.getDismissReason(reason)}`;
});
}
例えば、私のような何かをしたい:
// In this example, content is undefined as I am not and can not pass #content through from the HTML since it is not triggered by a button press like the demo
if (myCustomCondition == true) {
this.modalService.open(content).result.then((result) => {
this.closeResult = `Closed with: ${result}`;
}, (reason) => {
this.closeResult = `Dismissed ${this.getDismissReason(reason)}`;
});
}
[このplunkr](http://plnkr.co/edit/mPQQsRvlbBQj0r8LxsiX?p=previewを動作するはずです)? –
ちょっとPankaj。コードが動作しています。しかし、私の質問は、どのように私はコンポーネントからオープンコードをトリガーすることができますそれを変更することができますです。理由は私が "コンテンツ"へのアクセスを持っていないということです。if(myCondition == true){open(content)}更新された質問 – user172902
@ user172902に示されているように、if文を記述しますか?私は問題が何であるか理解していません。 – echonax