u010200767
图森破少年
2018-05-10 04:53
采纳率: 23.9%
浏览 795

angular的@output的问题

我有一个child-component:

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

@Component({
  selector: 'app-ng-content-child',
  template: `<button type="button" (click)="onclick()">Click me to fire a custom event</button>`
})
export class NgContentChildComponent implements OnInit {

  testData = 1;
  @Output()
  childEvent = new EventEmitter();

  constructor() { }
  ngOnInit() {}

  onclick() {
    this.childEvent.emit(this.testData);
  }
}

还有一个parent-component

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

@Component({
  selector: 'app-ng-content-parent',
  template: `<app-ng-content-child (childEvent)="onchildevent()"></app-ng-content-child>`
})
export class NgContentParentComponent implements OnInit {

  constructor() { }
  ngOnInit() {}

  onchildevent(data) {
    console.log(data);
  }
}

我从子组件output到父组件。。为什么console.log那句是undefined?
另外,必须在附件组件中嵌入子组件,且在子组件上加监听器才可以在父组件中获得子组件的值妈?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

相关推荐