最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Angular @OutputEventEmitter returns undefined - Stack Overflow

programmeradmin1浏览0评论

I have been trying to send data from a child component to a parent component in Angular. At the parent level, the prop keeps returning 'undefined.' I don't know what I'm missing.

CHILDponent.ts

@Output()
  onGetDropdownItems: EventEmitter<any> = new EventEmitter<any>();
  getDropdownItems() {
    this.onGetDropdownItems.emit(this.dropdownItems)
  }

  /**
   * Sets the data for the Items per page select dropdown.
   */
  @Input()
  dropdownItems: [
    {
      optionValue: "5",
      optionText: "5",
    },
    {
      optionValue: "10",
      optionText: "10",
    },
    {
      optionValue: "20",
      optionText: "20",
    }
  ]

PARENTponent.ts

  @Input()
  dropdownItems: any;

  getDropdownItems(e: any) {
    this.dropdownItems = e;
    console.log('DROPDOWN ITEMS: ', e);
  }

  ngOnInit() {
    this.getDropdownItems(this.dropdownItems);
  }

PARENTponent.html

<cbds-pagination 
    (onGetDropdownItems)="getDropdownItems($event)">
</cbds-pagination>

I have been trying to send data from a child component to a parent component in Angular. At the parent level, the prop keeps returning 'undefined.' I don't know what I'm missing.

CHILD.component.ts

@Output()
  onGetDropdownItems: EventEmitter<any> = new EventEmitter<any>();
  getDropdownItems() {
    this.onGetDropdownItems.emit(this.dropdownItems)
  }

  /**
   * Sets the data for the Items per page select dropdown.
   */
  @Input()
  dropdownItems: [
    {
      optionValue: "5",
      optionText: "5",
    },
    {
      optionValue: "10",
      optionText: "10",
    },
    {
      optionValue: "20",
      optionText: "20",
    }
  ]

PARENT.component.ts

  @Input()
  dropdownItems: any;

  getDropdownItems(e: any) {
    this.dropdownItems = e;
    console.log('DROPDOWN ITEMS: ', e);
  }

  ngOnInit() {
    this.getDropdownItems(this.dropdownItems);
  }

PARENT.component.html

<cbds-pagination 
    (onGetDropdownItems)="getDropdownItems($event)">
</cbds-pagination>
Share Improve this question asked Feb 5 at 18:28 user3472810user3472810 4491 gold badge5 silver badges14 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

The dropdownItems property`s value is defined as a type.

@Input()
dropdownItems = [
    {
      optionValue: "5",
      optionText: "5",
    },
    {
      optionValue: "10",
      optionText: "10",
    },
    {
      optionValue: "20",
      optionText: "20",
    }
  ]
发布评论

评论列表(0)

  1. 暂无评论