Angular html 中的通配符字符串比较
Angular wildcard string comparison in html
这可能是一个非常基本的问题,我知道各种方法来完成我想要的。但是,我想知道是否可以在 *ngIf 比较中使用通配符。
想象以下代码:
<ng-container *ngIf="test == 'teststring'">
</ng-container>
我想知道是否可以使用:
<ng-container *ngIf="test == '*teststring'">
</ng-container>
所以它可能是 0teststring 或 1teststring。
提前致谢!
您要找的是test.endsWith('teststring')
。然而,在模板中调用函数是不好的,因为它们在每次更新时都会被调用。使用 pure pipe 代替:
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({ name: 'endsWith', pure: true })
export class EndsWithPipe implements PipeTransform {
public transform(a_sString: string, a_sEndsWith: string): boolean {
return a_sString.endsWith(a_sEndsWith);
}
}
用法:
*ngIf="test | endsWith:'teststring'"
这可能是一个非常基本的问题,我知道各种方法来完成我想要的。但是,我想知道是否可以在 *ngIf 比较中使用通配符。
想象以下代码:
<ng-container *ngIf="test == 'teststring'">
</ng-container>
我想知道是否可以使用:
<ng-container *ngIf="test == '*teststring'">
</ng-container>
所以它可能是 0teststring 或 1teststring。
提前致谢!
您要找的是test.endsWith('teststring')
。然而,在模板中调用函数是不好的,因为它们在每次更新时都会被调用。使用 pure pipe 代替:
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({ name: 'endsWith', pure: true })
export class EndsWithPipe implements PipeTransform {
public transform(a_sString: string, a_sEndsWith: string): boolean {
return a_sString.endsWith(a_sEndsWith);
}
}
用法:
*ngIf="test | endsWith:'teststring'"