EventEmitter不在Angular 2中工作

时间:2016-08-27 04:04:14

标签: angular eventemitter

我正在尝试从父组件(例如described here in the angular 2 docs)侦听子组件上的事件,但该事件从未进入父组件。

我确信代码在发出事件的子代中贯穿此行:

this.onResultsRecieved.emit(true);

以下是我所涉及的所有代码:

父:

找到-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;
        }
  }
}

查找-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()函数?

请注意,this Stack Overflow answer在组件上包含events : ['update'],但我不知道那是什么,因为它不在the angular component interaction docs

1 个答案:

答案 0 :(得分:11)

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

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

如果你复制&amp;粘贴,请记住,您还拼写了“接收”字样。错误。 :]