使用ngIf检查字符串是否以某些字符开头 [英] Use ngIf checking if a string starts with certain character
本文介绍了使用ngIf检查字符串是否以某些字符开头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以通过使用角度来做到这一点?我尝试了两种方式,但都没有用.
Is it possible to do this by using angular? I have tried both ways and none of them work.
<div class="card-1" [ngStyle]="{'opacity':cardNumber[0] == '4' ? '0.5' : 'none' }"></div>
帮助
[ngStyle]="{'opacity':cardNumber.startsWith('4') ? '0.5' : 'none' }">
推荐答案
您最好为此创建一个管道:
You might as well create a pipe for that:
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'startsWith'
})
export class StartsWithPipe implements PipeTransform {
transform(fullText: string, textMatch: string): boolean {
return fullText.startsWith(textMatch);
}
}
并像这样使用它:
<div [ngStyle]="{'opacity': ('hello' | startsWith:'he') ? '0.5' : 'none' }">
Test
</div>
这是一个有效的Stackblitz示例:
https://stackblitz.com/edit/angular-nmjvu3
Here's a working Stackblitz example:
https://stackblitz.com/edit/angular-nmjvu3
这篇关于使用ngIf检查字符串是否以某些字符开头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文