EventEmitter不在Angular 2中工作
发布时间:2020-12-17 17:58:41 所属栏目:安全 来源:网络整理
导读:我正在尝试从像 described here in the angular 2 docs这样的父组件中侦听子组件上的事件,但事件从未进入父组件. 我确信代码在发出事件的子代中贯穿此行: this.onResultsRecieved.emit(真); 以下是我所涉及的所有代码: 家长: 找到-page.component.ts impo
我正在尝试从像
described here in the angular 2 docs这样的父组件中侦听子组件上的事件,但事件从未进入父组件.
我确信代码在发出事件的子代中贯穿此行: this.onResultsRecieved.emit(真); 以下是我所涉及的所有代码: 家长: 找到-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: ['',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包含事件:组件上的[‘update’],但我不知道是什么,因为它不在the angular component interaction docs 解决方法
在find-page.component.html中,您将事件绑定到常规div元素.您需要将事件绑定到find-form组件,因为它实际上包含您要从中接收事件的EventEmitter.
<find-form (onResultsReceived)="onResultsReceived($event)"></find-form> 如果你复制&粘贴,记住你也拼错’接收’错了. :] (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |