2017-03-14 85 views
0

我正在使用bg boostrap模式。在組件中訪問在模板中聲明的變量

我有具有訪問content對象的按鈕僅被定義在視圖

<button (click)="open(content)">Launch demo modal</button> 

open該方法是在組件中定義,但content對象僅在模板存在。

如何從組件內部撥打this.open(accessContentHere)

回答

0

你可以使用ViewChild裝飾器來做到這一點。在您的組件中,輸入ViewChild,然後設置template local variable的名稱作爲參數:

import {..., Component, OnInit, ViewChild } from '@angular/core' 

... 

export class YourComponent extends Component implements OnInit { 
    @ViewChild('content') content; 

    ngOnInit() { 
     this.open(content); 
    } 

    open(myContent) { //...} 

    ... 

}