2017-01-25 9 views
1

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">&times;</span> 
    </button> 
    </div> 
    <div class="modal-body"> 
    <p>One fine body&hellip;</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)}`; 
    }); 
} 

content is not defined

+0

[このplunkr](http://plnkr.co/edit/mPQQsRvlbBQj0r8LxsiX?p=previewを動作するはずです)? –

+0

ちょっとPankaj。コードが動作しています。しかし、私の質問は、どのように私はコンポーネントからオープンコードをトリガーすることができますそれを変更することができますです。理由は私が "コンテンツ"へのアクセスを持っていないということです。if(myCondition == true){open(content)}更新された質問 – user172902

+0

@ user172902に示されているように、if文を記述しますか?私は問題が何であるか理解していません。 – echonax

答えて

0

TemplateRefができるドキュメントによると、渡されたhttps://ng-bootstrap.github.io/#/components/modal

ので、これは

@ViewChild('content') content:TemplateRef; 

    open() { 
    this.modalService.open(this.content).result.then((result) => { 
     this.closeResult = `Closed with: ${result}`; 
    }, (reason) => { 
     this.closeResult = `Dismissed ${this.getDismissReason(reason)}`; 
    }); 
    } 
それはあなたがここにレプリケーションしてみてください可能性があり、私のために働いているようだ
+0

Angularは構文をはっきりと好まない。 @ViewChild( 'content')とcontentTemplateRefを追加するとすぐに次のエラーが表示されます。 インラインテンプレート:0:5原因:formGroupはFormGroupインスタンスを必要とします。 – user172902

+0

申し訳ありませんが、 ':'が修正されました。 –

+0

@ガンターを助けてくれてありがとう。私はまだ同じエラーが発生しています "formGroupはFormGroupインスタンスを期待しています。私は@ViewChild( 'content')コンテンツを入れた直後にTemplateRef を挿入しなければなりません。構文が正しく動作するにはTemplateRefとをインポートしなければなりませんでした。この例ではフォームを使用していないので、フォームグループや何らかの並べ替えが必要な理由はわかりません。 – user172902

関連する問題