首页 > 解决方案 > 模板解析错误角度 8

问题描述

我正在使用角度 8 我想显示一个带有角度材料的弹出窗口,这就是我所做的:

在我的 HTML 中:

<button (click)="myDia.open()">Open it</button>

Result: {{result | json}}

<my-dialog (result)="result = $event" #myDia>
  <h1 mat-dialog-title>Dialog Title</h1>
  <mat-dialog-content>
      Content
  </mat-dialog-content>
  <mat-dialog-actions>
      <button mat-button (click)="myDia.close()">Cancel</button>
      <button mat-button (click)="myDia.close({foo:'bar'})">Confirm</button>
  </mat-dialog-actions>
</my-dialog>

这是 Ts 文件:

@Component({
  selector: 'my-dialog',
  template: `
  <ng-template>
    <ng-content></ng-content>
  </ng-template>
  `
})


export class MyDialogComponent {
  @ViewChild(TemplateRef)
  private templateRef: TemplateRef<any>;

  @Output()
  result = new EventEmitter<boolean>();

  private dialogRef: MatDialogRef<any>;

  constructor(private dialog: MatDialog) {}

  open() {
      this.dialogRef = this.dialog.open(this.templateRef);

      this.dialogRef.afterClosed().subscribe(val => {
          this.result.emit(val);
      });
  }

  close(val: any) {
      this.dialogRef.close(val);
  }

}

我在这一行有一个问题:

  @ViewChild(TemplateRef)

错误是:

预期有 2 个参数,但得到 1 未提供“选择”的参数。

我在我的 app.module.ts 中添加了:

import { MyDialogComponent } from './pages/interfacage/test/test.component';

那么我该如何解决这个问题。

我正在使用这个 tuto路径

标签: javascripthtmlangular

解决方案


现在它在Angular8中工作

    @ViewChild(TemplateRef, {static: false})
    private templateRef: TemplateRef<any>;

https://stackblitz.com/edit/angular-material2-issue-yqcfpj?file=app/app.component.html



推荐阅读