我有一个孩子组件,可以将事件如此
@Output() setAdditionalCodeValue: EventEmitter<any> = new EventEmitter();
,HTML是
<mat-row *matRowDef="let row; columns: displayedColumns;" class="my-mat-cell" (click)="setAdditionalCodeValue.emit(row)">
</mat-row>
我的父html随后绑定到setAdditionalCodeValue这样的
<ng-container matColumnDef="additionalCode">
<th mat-header-cell *matHeaderCellDef mat-sort-header>Additional Code</th>
<td mat-cell *matCellDef="let element" class="grandParent" >
<mat-form-field class="type" let="i = index">
<input matInput (keyup)="toggleLookup($event, element)" (setAdditionalCodeValue)="updateAdditionalCodeHandler(element)" [(ngModel)]="countryLookupInput" autocomplete="off" (keydown.ArrowDown)="onDown()">
</mat-form-field>
<div *ngIf="element.expanded" class="parent">
<app-lookup-popup class="child" (closeLookup)="closeLookupHandler(element)" ></app-lookup-popup>
</div>
</td>
</ng-container>
和父组件看起来像
updateAdditionalCodeHandler(evt) {
console.log('Update Addition Code event received: ' + evt);
this.countryLookupInput = evt;
}
updateadeDiteAlcodeHandler没有被击中,因为一开始没有写在控制台上。
我的最终目标是更新'countryLookupInput'
属性具有从孩子的"行"参数发出的值。
奇怪地以相同的方式连接的'(closeLookup)="closeLookupHandler(element)"'
,去看!
问题在下面的元素中:
<app-lookup-popup class="child" (closeLookup)="closeLookupHandler(element)" ></app-lookup-popup>
修改的代码
<app-lookup-popup class="child" (closeLookup)="closeLookupHandler(element)" (setAdditionalCodeValue)="updateAdditionalCodeHandler(element)" ></app-lookup-popup>
和其他组件
<input matInput (keyup)="toggleLookup($event, element)"[(ngModel)]="countryLookupInput" autocomplete="off" (keydown.ArrowDown)="onDown()">
如果这是发出事件的组件
我认为您的代码中有两个问题,首先您在input
上处理了该事件,而不是您的孩子组件
我的意思是在这里 ->
<input matInput (keyup)="toggleLookup($event, element)" (setAdditionalCodeValue)="updateAdditionalCodeHandler(element)"
而不是这里 ->
<app-lookup-popup class="child" (closeLookup)="closeLookupHandler(element)" ></app-lookup-popup>
,第二期是您需要使用$event
来访问发射的值
因此,以下修改的代码应起作用
<ng-container matColumnDef="additionalCode">
<th mat-header-cell *matHeaderCellDef mat-sort-header>Additional Code</th>
<td mat-cell *matCellDef="let element" class="grandParent" >
<mat-form-field class="type" let="i = index">
<input matInput (keyup)="toggleLookup($event, element)" [(ngModel)]="countryLookupInput" autocomplete="off" (keydown.ArrowDown)="onDown()">
</mat-form-field>
<div *ngIf="element.expanded" class="parent">
<app-lookup-popup class="child" (closeLookup)="closeLookupHandler(element)"
(setAdditionalCodeValue)="updateAdditionalCodeHandler($event)" ></app-lookup-popup>
</div>
</td>
</ng-container>