EventEmitter在Angular 2中不起作用

本杰明·巴金斯(Benjamin Baggins)

我正在尝试从有角的2文档中此处描述的父组件监听子组件上的事件,但是该事件从未传递给父组件。

我肯定知道代码在发出事件的子代中通过此行运行:

this.onResultsRecieved.emit(true);

这是我所有涉及的代码:

上级:

find-page.component.ts

import { Component } from '@angular/core';
import { NavbarComponent } from '../shared/navbar.component';
import { FindFormComponent } from '../find-page/find-form.component';

@Component({
   selector: 'find-page',
   templateUrl: 'app/find-page/find-page.component.html',
   styleUrls: ['app/find-page/find-page.component.css' ],
   directives: [ FindFormComponent ]
})
export class FindPageComponent {
   showResults = false;

     onResultsRecieved(recieved: boolean) {
        if ( recieved ) {
           this.showResults = true;
        }else {
           this.showResults = false;
        }
  }
}

find-page.component.html:

<div id="find-page">
   <find-form></find-form>
</div>
<div (onResultsRecieved)="onResultsRecieved($event)" *ngIf="showResults" id="results-page">
</div>

儿童:

find-form.component.ts

import { Component, OnInit, ViewChild, ElementRef, EventEmitter, Output } from '@angular/core';
import {  REACTIVE_FORM_DIRECTIVES,
  FormGroup,
  FormBuilder,
  Validators,
  ControlValueAccessor
} from '@angular/forms';
import { ResultService } from '../services/result.service';
import { Result } from '../result'
import { NumberPickerComponent } from './number-picker.component';
import { DistanceUnitsComponent } from './distance-units.component';
import { MapDemoComponent } from '../shared/map-demo.component';
import { AreaComponent } from './area-picker.component';
import { GoComponent } from '../shared/go.component';
import { HighlightDirective } from '../highlight.directive';

@Component({
  selector: 'find-form',
  templateUrl: 'app/find-page/find-form.component.html',
  styleUrls: ['app/find-page/find-form.component.css'],
  providers: [ResultService],
  directives: [REACTIVE_FORM_DIRECTIVES,
    NumberPickerComponent,
    DistanceUnitsComponent,
    MapDemoComponent,
    AreaComponent,
    GoComponent]
})
export class FindFormComponent implements OnInit {
  findForm: FormGroup;
  submitted: boolean; // keep track on whether form is submitted
  events: any[] = []; // use later to display form changes
  @ViewChild('keywordsInput') keywordsInput;
  @Output() onResultsRecieved = new EventEmitter<boolean>();
  results: Result[];

  constructor(private resultService: ResultService,
    private formBuilder: FormBuilder,
    el: ElementRef) { }


  goClicked(): void {
    this.getResults();

  }

  getResults(): void {
    this.results = this.resultService.getResults();
    console.log(this.results);
    this.onResultsRecieved.emit(true);
  }

  ngOnInit() {
    this.findForm = this.formBuilder.group({
      firstname: ['', [Validators.required, Validators.minLength(5)]],
      lastname: ['', Validators.required],
      keywords: [],
      area: ['', Validators.required],
      address: this.formBuilder.group({
        street: [],
        zip: [],
        city: []
      })
    });
    this.findForm.valueChanges.subscribe(data => console.log('form changes', data));
  }

  focusKeywordsInput() {
    this.keywordsInput.nativeElement.focus();
  }

  save(isValid: boolean) {
    this.submitted = true;
    console.log(isValid);
    console.log(this.findForm);
  }
}

为什么事件不触发父对象的onResultsRecieved()函数执行?

请注意,此堆栈溢出答案包含events : ['update']在组件中,但我不知道那是什么,因为它不在角度组件交互文档中

阿巴宾

在中find-page.component.html,您已将事件绑定到常规div元素。您需要将事件绑定到find-form组件,因为它实际上包含您要从中接收事件的EventEmitter。

<find-form (onResultsReceived)="onResultsReceived($event)"></find-form>

如果您将其复制并粘贴到其中,请记住您也拼写“接收”错误。:]

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章