Gilberto Langarica
Gilberto Langarica

Reputation: 75

Use ngIf checking if a string starts with certain character

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' }">

Upvotes: 1

Views: 2904

Answers (1)

maxime1992
maxime1992

Reputation: 23813

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

Upvotes: 4

Related Questions