EventEmitter 在 Angular 2 中不工作
EventEmitter not working in Angular 2
我正在尝试从 parent 组件(如 described here in the angular 2 docs)监听 child 组件上的事件,但该事件从未到达 parent。
我确定代码在发出事件的 child 中贯穿这一行:
this.onResultsRecieved.emit(true);
这是我涉及的所有代码:
Parent:
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>
Child:
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);
}
}
为什么事件没有触发parent的onResultsRecieved()函数执行?
请注意 includes events : ['update']
on the component but I don't know what that is because it is not in the angular component interaction docs
在 find-page.component.html
中,您将事件绑定到常规 div 元素。您需要将事件绑定到 find-form
组件,因为它实际上包含您要从中接收事件的 EventEmitter。
<find-form (onResultsReceived)="onResultsReceived($event)"></find-form>
如果您复制并粘贴它,请记住您也拼写了 'receive' 错误。 : ]
我正在尝试从 parent 组件(如 described here in the angular 2 docs)监听 child 组件上的事件,但该事件从未到达 parent。
我确定代码在发出事件的 child 中贯穿这一行:
this.onResultsRecieved.emit(true);
这是我涉及的所有代码:
Parent:
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>
Child:
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);
}
}
为什么事件没有触发parent的onResultsRecieved()函数执行?
请注意events : ['update']
on the component but I don't know what that is because it is not in the angular component interaction docs
在 find-page.component.html
中,您将事件绑定到常规 div 元素。您需要将事件绑定到 find-form
组件,因为它实际上包含您要从中接收事件的 EventEmitter。
<find-form (onResultsReceived)="onResultsReceived($event)"></find-form>
如果您复制并粘贴它,请记住您也拼写了 'receive' 错误。 : ]