简体   繁体   English

在组件中访问模板中声明的变量

[英]access in a component a variable that is declared in the template

I am using bg boostrap modal. 我正在使用bg boostrap模态。

I have a button that that has access to the content object that is defined only in the view 我有一个可以访问仅在视图中定义的content对象的按钮

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

the open method is defined in the component, but the content object only exists in the template. open方法在组件中定义,但是content对象仅存在于模板中。

How can I call this.open(accessContentHere) from inside my component ? 如何从组件内部调用this.open(accessContentHere)

You can use the ViewChild decorator for this. 您可以为此使用ViewChild装饰器。 In your component, import ViewChild , then set the name of the template local variable as the parameter: 在您的组件中,导入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) { //...}

    ...

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM