子组件中的Angular 4调用父方法

Iho*_*avs 45 angular

我想在Angular 4中的子组件中调用父方法(deletePhone).我该如何正确地做到这一点?

我的父组件看起来像:

export class ContactInfo implements OnInit {
    phoneForm: FormGroup;
    phones: Phone[];


    constructor(private fb: FormBuilder,
            private userService: UserService) {
    }

    ngOnInit() {
        this.userService.getDataPhones().subscribe(
            phones => {
                this.phones = phones;
            });

        this.phoneForm = this.fb.group({
            phone: ['', [Validators.pattern(PHONE_PATTERN)]]
        });
    }

    deletePhone(phone: Phone) {
        this.userService.deleteUserPhone(phone)
            .subscribe(res => {
                let index = this.phones.indexOf(phone);
                if (index > -1) {
                    this.phones.splice(index, 1);
                }
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

Gün*_*uer 101

import { Output, EventEmitter } from '@angular/core'; 

...

class ChildComponent {
  @Output() someEvent = new EventEmitter<string>();

  callParent() {
    this.someEvent.next('somePhone');
  }
}
Run Code Online (Sandbox Code Playgroud)

ContactInfo模板中

<child-component (someEvent)="deletePhone($event)"
Run Code Online (Sandbox Code Playgroud)

  • 不只是初学者,答案是不完整的。应编辑答案以包括 Sergey 的附录。 (7认同)
  • 对于begginers,您需要在'@ angular/core'中添加"import {Output,EventEmitter}" (6认同)
  • 除了彻底和乐于助人之外,没有其他原因。 (4认同)
  • 您可以添加目标类型,例如"new EventEmitter <boolean>()":) (3认同)
  • 我认为 someEvent.emit() 是首选,并且 .next() 已弃用。/sf/ask/2508840351/ Between-eventemitter-next-and-eventemitter-emit-in-angular-2 (3认同)
  • 我不明白为什么每个答案都需要列出导入。您可以在 Angular 文档中查找它们。**当前文件中未声明的所有内容**都需要导入。 (2认同)

Nit*_*hav 13

这对我有用(例如来自官方文档):

https://angular.io/api/core/EventEmitter#examples

儿童:

@Component({
  selector: 'zippy',
  template: `
  <div class="zippy">
    <div (click)="toggle()">Toggle</div>
    <div [hidden]="!visible">
      <ng-content></ng-content>
    </div>
 </div>`})
export class Zippy {
  visible: boolean = true;
  @Output() open: EventEmitter<any> = new EventEmitter();
  @Output() close: EventEmitter<any> = new EventEmitter();

  toggle() {
    this.visible = !this.visible;
    if (this.visible) {
      this.open.emit(null); //emit event here
    } else {
      this.close.emit(null);
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

家长:

<zippy (open)="onOpen($event)" (close)="onClose($event)"></zippy>
Run Code Online (Sandbox Code Playgroud)


Ale*_*rov 8

我不喜欢像@Output() 这样的样板代码。我找到了另一个解决方案,只需传递具有任意数量匿名函数的对象

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-parent',
  styleUrls: ['./parent.component.css'],
  template: `
  <app-child [parentApi]="getParentApi()"></app-child>
`,
})
export class ParentComponent implements OnInit {

  getParentApi(): ParentComponentApi {
    return {
      callParentMethod: (name) => {
        this.parentMethod(name)
      }
    }
  }

  constructor() { }

  ngOnInit() {
  }

  parentMethod(name: string) {
    console.log(`Hello ${name} from parent`)
  }
  
}

export interface ParentComponentApi {
  callParentMethod: (string) => void
}
Run Code Online (Sandbox Code Playgroud)

还有孩子:

import { Component, OnInit, Input } from '@angular/core';
import { ParentComponentApi } from '../parent/parent.component';

@Component({
  selector: 'app-child',
  template: `<button (click)="callParent()">call parent</button>`,
  styleUrls: ['./child.component.css']
})
export class ChildComponent implements OnInit {

  @Input() parentApi: ParentComponentApi

  constructor() { }

  callParent() {
    this.parentApi.callParentMethod("child")
  }

  ngOnInit() {

  }

}
Run Code Online (Sandbox Code Playgroud)

我认为这样做很安全,不是吗?

  • 我喜欢这个主意。不幸的是在 Angular 7 中不起作用。给出模板解析错误。 (4认同)

Ice*_*erg 6

这比您想象的要简单。关键是将父方法传递给子 @Input 属性在线测试

父组件

@Component({
  selector: 'my-app',
  template: `
    <h1>Parent:</h1>
    <p>Parent counting: {{this.count}}</p>

    <child-comp [childInc]="this.inc"></child-comp>
  `
})
export class AppComponent {
  name = "I'm Parent";

  count = 0;

  inc = () => {
    this.count++;
  }
}

Run Code Online (Sandbox Code Playgroud)

子组件

@Component({
  selector: 'child-comp',
  template: `
  <h1>Child:</h1> 
  <button (click)="childInc()">Click me!</button>
  `
})
export class ChildComponent  {
  name = "I'm child"

  @Input() childInc: () => void
}
Run Code Online (Sandbox Code Playgroud)

inc = () => {...}在parent中使用了概念,它可以记住正确的this。如果您使用这个inc(){...}概念,那么您需要将parent绑定this[childInc]="this.inc.bind(this)".