0

我有一个发出事件的子组件

@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;

}

updateAdditionalCodeHandler 没有被击中,因为没有任何内容写入控制台开始。

我的最终目标是'countryLookupInput' 使用子项的“行”参数发出的值更新属性。

奇怪的'(closeLookup)="closeLookupHandler(element)"'是,它以同样的方式连接起来,去看看吧!

4

2 回答 2

1

问题出在以下元素中:

 <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()">

如果这是发出事件的组件

于 2019-05-24T12:33:54.580 回答
0

我认为您的代码中有两个问题,首先您在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> 
于 2019-05-24T13:09:26.430 回答