【问题标题】:access in a component a variable that is declared in the template在组件中访问在模板中声明的变量
【发布时间】:2017-03-14 12:29:28
【问题描述】:

我正在使用 bg boostrap 模态。

我有一个按钮可以访问仅在视图中定义的content 对象

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

组件中定义了open方法,但content对象只存在于模板中。

如何从我的组件内部调用this.open(accessContentHere)

【问题讨论】:

    标签: angular modal-dialog ng-bootstrap


    【解决方案1】:

    您可以为此使用 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) { //...}
    
        ...
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-15
      • 1970-01-01
      • 1970-01-01
      • 2011-07-29
      • 2016-04-15
      • 2015-03-31
      相关资源
      最近更新 更多