使用 ngIf 检查字符串是否以某个字符开头 [英] Use ngIf checking if a string starts with certain character

查看:36
本文介绍了使用 ngIf 检查字符串是否以某个字符开头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以通过使用 angular 来做到这一点?我已经尝试了两种方法,但都没有奏效.

<div class="card-1" [ngStyle]="{'opacity':cardNumber[0] == '4' ? '0.5' : 'none' }"></div>

帮助

 [ngStyle]="{'opacity':cardNumber.startsWith('4') ? '0.5' : 'none' }">

解决方案

您不妨为此创建一个管道:

import { Pipe, PipeTransform } from '@angular/core';@管道({名称:'开始于'})导出类 StartsWithPipe 实现 PipeTransform {变换(全文:字符串,textMatch:字符串):布尔{返回 fullText.startsWith(textMatch);}}

并像这样使用它:

<div [ngStyle]="{'opacity': ('hello' | startsWith:'he') ? '0.5' : 'none' }">测试

这是一个有效的 Stackblitz 示例:
https://stackblitz.com/edit/angular-nmjvu3

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>

Help

 [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);
  }
}

And use it like that:

<div [ngStyle]="{'opacity': ('hello' | startsWith:'he') ? '0.5' : 'none' }">
  Test
</div>

Here's a working Stackblitz example:
https://stackblitz.com/edit/angular-nmjvu3

这篇关于使用 ngIf 检查字符串是否以某个字符开头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆