代码之家  ›  专栏  ›  技术社区  ›  Emile Cantero

在角5中触发从组件到不可见组件的事件

  •  0
  • Emile Cantero  · 技术社区  · 6 年前

    我在一个组件(child.html)中有一个div,我将触发另一个(parent.html)的click事件,将布尔值从false转换为true。我知道使用HTML作为 <child-component [Event]></child-component> 但是用户必须看不到这个孩子。

    是否可以从app.css中将css添加到以使其不可见?? 我不确定是否可以使它成角度,下面是我的尝试:

    child.html:

       <div class="notif-filter" (click)="hideNotif(event)"> </div>
    

     public hideNotif(event) {
        console.log(event, 'hideNotif is fired !!');
         this.hideMe = true;
        // this.feedIsVisible = false;
       }
    

      <child-component></child-component> 
    

     app-child: { display: none }
    

         @input event: Event (comming from the child)
    
        lockScreen(): void {
        this.screenLocked = true;
        this.feedIsVisible = false;    ==> *This should turn to true depending on 
        the click event in the child.html*
        this.nav.setRoot('ConnexionPage').then();
      }
    
    2 回复  |  直到 6 年前
        1
  •  2
  •   Anuradha Gunasekara    6 年前

    第一次阅读 Angular Component Interaction .

    然后您将知道组件之间有4种主要的通信方式。

    在您的场景中,您可以使用任何您想要的方法。

    看看这个例子

    子组件.ts

    import { Component, Output, EventEmitter } from '@angular/core';
    
    @Component({
      selector: 'app-child',
      templateUrl: './child.component.html',
      styleUrls: ['./child.component.css']
    })
    export class ChildComponent {
      public show: boolean;
    
      @Output()
      public childButtonClick = new EventEmitter<boolean>();
    
      constructor() {
        this.show = true;
      }
    
      public onClick(): void {
        this.show = !this.show;
        this.childButtonClick.emit(this.show);
      }
    
    }
    

    child.component.html(子组件.html)

    <button (click)="onClick()">Child button</button>
    

    应用组件.ts

    import { Component } from '@angular/core';
    
    @Component({
      selector: 'my-app',
      templateUrl: './app.component.html',
      styleUrls: ['./app.component.css']
    })
    export class AppComponent {
      public show: boolean;
    
    
      constructor() {
        this.show = true;
      }
      public onChildButtonClick(value): void {
        this.show = value;
      }
    }
    

    app.component.html(应用程序组件.html)

    <app-child (childButtonClick)="onChildButtonClick($event)"></app-child>
    
    
    <div *ngIf="show">
      This will be hidden when you click the button
      And I'm using ngIf directive
    </div>
    
    <br>
    <br>
    
    <div [ngClass]="{'hide-me': !show}">
      This will be hidden when you click the button
      And I'm using ngClass directive
    </div>
    
    <br>
    <div>
      show value: {{show}}
    </div>
    

    应用程序组件.css

    .hide-me {
      display: none
    }
    

    在这里我用了 父级侦听子事件 方法。 我创造了一个 stackblitz 为了这个。

        2
  •  0
  •   Akj    6 年前

    Working Example

    HTML:

    你好.componemt.html

    <button (click)="onClick()">hello button</button>
    

    app.component.html(应用程序组件.html)

    <hello (childClick)="onChildButtonClick($event)"></hello>
    
    {{show}}
    

    TS:

    您好,component.ts:

    import { Component,Input, Output, EventEmitter, } from '@angular/core';
    
    @Component({
     selector: 'hello',
      templateUrl: './hello.component.html',
      styleUrls: [ './app.component.css' ]
    })
    export class HelloComponent  {
    
      public show: boolean;
    
      @Output()
      public childClick = new EventEmitter<boolean>();
    
      constructor() {
        this.show = true;
      }
    
      public onClick(): void {
        this.show = !this.show;
        this.childClick.emit(this.show);
      }
    }
    

    应用组件.ts:

    import { Component } from '@angular/core';
    
    @Component({
      selector: 'my-app',
      templateUrl: './app.component.html',
      styleUrls: [ './app.component.css' ]
    })
    export class AppComponent  {
      name = 'Angular 6';
       public show: boolean;
    
    
      constructor() {
        this.show = true;
      }
      public onChildButtonClick(value): void {
        this.show = value;
      }
    }