代码之家  ›  专栏  ›  技术社区  ›  Buddhika Chathuranga

两个组件之间的角度通信

  •  -1
  • Buddhika Chathuranga  · 技术社区  · 8 年前


    在我的应用程序中,有一个组件有一个子组件。这个组件中有一个函数(在父组件中,而不是子组件中),我想使用子组件中的按钮来调用这个函数。

    此图说明了我的组件的格式。

    enter image description here

    @Output . 但我没办法。

    我的代码就是这样组织的。

    父组件-Component.ts文件

    import { Component, OnInit } from '@angular/core';
    
    @Component({
      selector: 'app-teacher-home',
      templateUrl: './teacher-home.component.html',
      styleUrls: ['./teacher-home.component.scss']
    })
    export class TeacherHomeComponent implements OnInit {
    
      constructor() { }
    
      ngOnInit() {
      }
    
      formView: boolean = false;
    
      toggleForm(){
        this.formView = !this.formView;
      }
    }
    

    父组件-component.html文件

    <div>
        <child-compnent></child-compnent>
    </div>
    

    <div>
        <button>Toggle Form view</button>
    </div>
    

    toggleForm() 单击子组件中的按钮时父组件的。

    4 回复  |  直到 7 年前
        1
  •  4
  •   Lia    8 年前

    Understanding @Output and EventEmitter in Angular

    子组件:

    @Component({
      selector: 'app-child',
      template: `<button (click)="sendToParent('hi')" >sendToParent</button> `
    })
    export class AppChildComponent {
      @Output() childToParent = new EventEmitter;
    
      sendToParent(name){
        this.childToParent.emit(name);
      }
    }
    

    父组件:

    @Component({
      selector: 'my-app',
      templateUrl: './app.component.html',
      styleUrls: [ './app.component.css' ]
    })
    export class AppComponent  {
      toggle(){
        console.log('toggle')
      }
    
    }
    

    <app-child (childToParent)="toggle($event)"></app-child>
    

    DEMO

        2
  •  4
  •   Yashwardhan Pauranik kizu    8 年前

    1. 是在子组件内创建一个事件,然后给它一个回调,如下所示:

      @Output('eventName') buttonPressed = new EventEmitter();

    打电话给我 buttonPressed.emit() 当您希望触发事件时

    在父端,它将如下所示:

    <div>
        <child-compnent (eventName)="toggleForm()"></child-compnent>
    </div>
    
    1. 另一种方法是创建一个共享服务,其中包含两个组件的共享函数和数据
        3
  •  3
  •   Yashwardhan Pauranik kizu    8 年前

    你需要使用 @Output 在子组件和 发出 当当前按钮在孩子体内单击时发生的事件。

    <div>
        <button (click)="childButtonClicked()">Toggle Form view</button>
    </div>
    

    子组件.ts

    export class ChildComponent {
      @Output triggerToggle: EventEmitter<any> = new EventEmitter<any>();
    
      ...
       childButtonClicked() {
         this.triggerToggle.emit(true);
       }
      ...
    }
    

    父组件

    <div>
        <child-compnent (triggerToggle)="toggleForm()"></child-compnent>
    </div>
    
        4
  •  2
  •   Sarthak Aggarwal    8 年前

    你可以用 EventEmitter 听子组件中的事件。

    父组件.ts

    toggleForm($event) {} 
    

    父组件.html

    <div>
        <child-compnent  (trigger)="toggleForm($event)" ></child-compnent>
    </div>
    

    @Output() trigger : EventEmitter<any> = new EventEmitter<any>();
    
    buttonClick(){
      this.trigger.emit('click');
    }
    
    推荐文章