私はangular2アプリケーションを開発しようとしています。私はページ上の各カードフッターに9つのボタンを持っています。任意のボタンまたはアンカータグをクリックすると、ブートストラップのモーダルが開きますが、クリックごとに内容が異なる必要があります。モーダルのテンプレートは、クリックされた各ボタンごとにコンテンツが異なることを除けば、同じでなければなりません。今は、同じテンプレートと内容のすべてのボタンでモーダルが開きます。 IDとボタンを関連付けることで、クリックが同じモーダルテンプレートでも異なるコンテンツになるようにします。ボタンクリックで異なる内容のモーダルを動的に開く
ここに私のコードはここsample.component.html
<div class="row">
<div class="col-md-4">
<div class="card">
<div class="card-content">
<div class="card-header-orange">
<h1 class="card-heading">Card Header Blue</h1>
</div>
<div class="card-body">
<p class="card-p">
There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour,
or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there
isn't anything embarrassing hidden in the middle of text.
</p>
</div><br>
<div class="card-footer">
<button (click)="openModal(template)" class="btn btn-primary">Learn More</button>
</div>
</div>
</div>
</div>
<div class="col-md-4">
<div class="card">
<div class="card-content">
<div class="card-header-orange">
<h1 class="card-heading">Card Header Blue</h1>
</div>
<div class="card-body">
<p class="card-p">
There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour,
or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there
isn't anything embarrassing hidden in the middle of text.
</p>
</div><br>
<div class="card-footer">
<button (click)="openModal(template)" class="btn btn-primary">Learn More</button>
</div>
</div>
</div>
</div>
<div class="col-md-4">
<div class="card">
<div class="card-content">
<div class="card-header-orange">
<h1 class="card-heading">Card Header Blue</h1>
</div>
<div class="card-body">
<p class="card-p">
There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour.
</p>
</div><br>
<div class="card-footer">
<button (click)="openModal(template)" class="btn btn-primary">Learn More</button>
</div>
</div>
</div>
</div>
</div>
にあります私のモーダルテンプレートです:
<ng-template #template>
<div class="modal-header">
<h4 class="modal-title pull-left">Modal</h4>
<button type="button" class="close pull-right" aria-label="Close" (click)="modalRef.hide()">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
This is a modal.
</div>
動的にコンテンツを変更するということは、各ボタンに10個のモーダルを作成する代わりに、1つのモーダルを持つ必要があり、クリックされたボタンに応じてそのモーダルの内容でなければならないということです。どのボタンをクリックしてそのコンポーネントを呼び出すのですか? – kam
はい、これは動的コンポーネントローダーで実現できます。ボタンをクリックするたびに、ロードするコンポーネント名をモーダルへの入力として渡すだけで、コンポーネントローダーを使用してコンポーネントをロードすることができます(基本的にはモーダルの本体です) –
コンポーネントローダーの説明で私の答えを更新 –