代码之家  ›  专栏  ›  技术社区  ›  Tanzeel

如何在单击事件时调用角度分量[角度]

  •  0
  • Tanzeel  · 技术社区  · 6 年前

    this . 我有一个按钮,我想从那里调用一个用户定义的方法 onPress .

    app.component.html

    <div class="dls-menu-item" style="float: right;">
        <button (click)="onPress()"></button>
    </div>
    
    <div id="comp-render">
            <-----Here i want that component to be rendered
    </div>
    

    import { Component, OnInit } from '@angular/core';
    
    @Component({
        ...
    })
    export class AnalyticsComponent implements OnInit {
        constructor() {}
    
        ngOnInit() {}
    
    
        onPress() {
            console.log("clicked");
            document.querySelector('#comp-render').innerHTML='<object type="text/html" data="mycomp.html" ></object>';
        }
    }
    

    在哪里? mycmp.html 是:

    <h1>Hello</h1>
    <p>This is a custom component.</p>
    <button>Click</button>
    

    谁能告诉我怎么做吗。

    1 回复  |  直到 6 年前
        1
  •  5
  •   Patricio Vargas    6 年前
    <div class="dls-menu-item" style="float: right;">
        <button (click)="onPress()"></button>
    </div>
    
    <div id="comp-render" *ngIf="display">
        <mycomp></mycomp>
    </div>
    

    应用程序组件.ts文件

     ...
     display = false;
     onPress() {
       this.display = true;
       /*if you want the component to show and hide on click pressed, use 
       use this line
       this.display = !this.display;*/
     }
    

    工作代码:

    https://stackblitz.com/edit/angular-d21pkn

    推荐文章