Angular 检查 `Template` 上的字符串模式

Angular check string pattern on `Template`

我正在尝试向我的 *ngIf 添加一个条件,只有当我的模板中的字符串以 .txt 结尾时它才会显示。

我试过了,但没用:

<div *ngIf="myfilename.endsWith('.txt')">...</div>

我知道上面的方法行不通,所以我的问题是:

我怎样才能只显示 .txt 个文件名?

最好在您的 component.ts 中检查一下,例如:

app.component.ts

import { Component, OnInit  } from '@angular/core';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent impelemnts OnInit  {
  myfilename: string;
  isTxt: boolean = false;

  ngOnInit() {
    this.isTxt = this.myfilename.endsWith(".txt")
  }
}

app.component.html

<div *ngIf="isTxt">...</div>