2017-01-23 1 views
1

私はこのスニペットを* ngForから取得していますので、何回も入力されています。すべてのプロファイルは、一意のIDを持っていると私は、このボタンをpreesときにそれを削除する:ng2-bs3-modalにデータを渡すには?

<a data-toggle="modal" data-target="#deleteProfile" (click)="deleteProfile.open()" role="button" style="color:red;"><i class="fa fa-trash-o" aria-hidden="true"></i> Delete</a> 

のHTMLモーダル:「はい」ボタンをクリックしたときに

<modal #deleteProfile> 
    <modal-header [show-close]="true"> 
    <h4 class="modal-title">Delete Profile</h4> 
    </modal-header> 
    <modal-body> 
    <div class="text-center"> 
     Are you sure you want to delete this profile? 
    </div> 
    </modal-body> 
    <modal-footer> 
    <div class="control-group confirm-buttons"> 
     <div class="row text-center"> 
     <div class="col-md-6"> 
      <button type="button" class="btn btn-cancel" data-dismiss="modal" (click)="closeDeleteProfile()">No</button> 
     </div> 
     <div class="col-md-6"> 
      <button type="button" class="btn btn-confirm" (click)="deleteProfile()">Yes</button> 
     </div> 
     </div> 
     <div class="col-md-12 text-center"> 
     <small>This is the footer</small> 
     </div> 
    </div> 
    </modal-footer> 
</modal> 

が、これが呼び出されます。

deleteProfile(id: string) { 
this.modalDeleteProfile.dismiss(); 
this.profileService.delete(id) 
    .subscribe(
    // data => console.log(data), 
    // error => console.log(error) 
); 
this.router.navigateByUrl('/dashboard'); 
} 

このIDをモーダルに渡して、上記のコードがIDを取得してプロファイルを削除できるようにするにはどうすればよいですか?

これは私が使用しているモーダルです:https://github.com/dougludlow/ng2-bs3-modal

+0

これが可能かどうかはわかりません。同じモーダルを使用しました。私はちょうどすぐに余分な方法を使用しました、それはちょうど数行でした。だから私はあなたが達成しようとしているものを達成しようとすることを気にしなかった。この質問に続きます:) – Alex

+0

@ AJT_82あなたは例を挙げることができますか? –

+0

の "余分な方法?" – Alex

答えて

2

をコメントでOPが要求した1として、ここで問題に代替ソリューションです。問題は、コンポーネントに余分なメソッドを追加することで解決できます。

* ngForを使用して配列を反復処理したとします。代わりに、直接モーダルを開くためのボタンを使用しての、のは、プロファイルのIDを渡す代わりにする方法を、開いてみましょうので、あなたの反復は次のようになります。

<table> 
    <tr *ngFor="let profile of profiles"> 
    <td>{{profile.id}}</td> 
    <td>{{profile.name}}</td> 
    <td><button (click)="openDeleteModal(profile.id)">Delete Profile</button</td> 
    </tr> 
</table> 

その後openDeleteModal -methodではなく、モーダルを開くだろうidをコンポーネント内のローカル変数にバインドした後のウィンドウです。

// declare an extra variable that can be used in deletion 
idForDeletingProfile; 

openDeleteModal(id) { 
    // here we bind the chosen id, so that we can use it in your delete-method 
    this.idForDeletingProfile = id; 
    this.modalDeleteProfile.open() 
} 

その後、我々は、私はちょうどボタンを表示するために短縮しているあなたのモーダル、持っている:

<modal #deleteProfile> 
    <!-- more code here --> 
    <button type="button" class="btn btn-cancel" data-dismiss="modal" (click)="closeDeleteProfile()">No</button> 
    <button type="button" class="btn btn-confirm" (click)="deleteProfile()">Yes</button> 
    <!-- more code here --> 
</modal> 

を、ユーザーがdeleteProfile() - ボタンをクリックした場合、我々は、前idForDeletingProfileで選択されたIDを格納しています削除のために使用できるようになりました。

deleteProfile() { 
    this.modalDeleteProfile.dismiss(); 
    // use the local variable here! 
    this.profileService.delete(this.idForDeletingProfile) 
    .subscribe(data => { 
     console.log(data); 
     this.router.navigateByUrl('dashboard'); 
    }); 
} 
+0

これは簡単にハックしているようです...あなたの方法を提供するためにあなたの努力に感謝します。何時間もプログラミングすると心が詰まる... –

+0

あなたは大歓迎です!お役に立てて嬉しいです! :) – Alex

+0

* ngForループからプロファイルを削除するためにfunctionallityを追加しました。ここでは、単純なハウツーで、作業コードではないplunkrへのリンクがあります:https://plnkr.co/edit/qFmpwQrNrOndVFORbZKX? –

関連する問題