1

This might be a very basic question and I know various ways to accomplish what I want. But, I would like to know if it is possible to use wildcards in *ngIf comparisons.

Imagine following code:

<ng-container *ngIf="test == 'teststring'">

</ng-container>

I would like to know if it is possible to for example use:

<ng-container *ngIf="test == '*teststring'">

</ng-container>

So that it could be 0teststring or 1teststring.

Thank you in advance!

1 Answer 1

3

What you're looking for is test.endsWith('teststring'). However, calling functions in the template is bad, because they are called on every tick. Use a pure pipe instead:

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

Usage:

*ngIf="test | endsWith:'teststring'"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.