代码之家  ›  专栏  ›  技术社区  ›  user3472810

Angular@Output/EventEmitter返回undefined

  •  1
  • user3472810  · 技术社区  · 4 月前

    我一直在尝试在Angular中将数据从子组件发送到父组件。在父级,道具不断返回“undefined”我不知道我错过了什么。

    儿童内容

    @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.com组件

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

    PARENT.com组件.html

    <cbds-pagination 
        (onGetDropdownItems)="getDropdownItems($event)">
    </cbds-pagination>
    
    1 回复  |  直到 4 月前
        1
  •  1
  •   Naren Murali    4 月前

    这个 dropdownItems 属性的值被定义为类型。

    @Input()
    dropdownItems: any = [
        {
          optionValue: "5",
          optionText: "5",
        },
        {
          optionValue: "10",
          optionText: "10",
        },
        {
          optionValue: "20",
          optionText: "20",
        }
      ]