Angular - using template variables in @if

Discussão em 'Angular' iniciado por Sachin, Dezembro 10, 2024.

  1. Sachin

    Sachin Guest

    This only works when I assign 'this.signal().object?.nestedObject' to a variable in a class, in Angular.

    Type Narrowing Does Not Work

    @if(isTypeA(this.signal().object?.nestedObject)) {
    {{ this.signal().object?.nestedObject.TypeAUniqueProperty }}
    } @else if (isTypeB(this.signal().object?.nestedObject)){
    {{ this.signal().object?.nestedObject.TypeBUniqueProperty }}
    }


    Type Narrowing Works

    @if(isTypeA(variable)) {
    {{ variable.TypeAUniqueProperty }}
    } @else if (isTypeB(variable)){
    {{ varble.TypeBUniqueProperty }}
    }


    Is it possible to assign this.signal().object?.nestedObject to an Angular template variable. It used to be possible with the old ngIf? This elimnates the need to set up variables in the class

    UPDATE

    I could only do this, by using an outer @if.

    @if(this.signal().object?.nestedObject; as templateVariable) {
    @if(isTypeA(templateVariable)) {
    {{ templateVariable.TypeAUniqueProperty }}
    } @else if (isTypeB(templateVariable)){
    {{ templateVariablet.TypeBUniqueProperty }}
    }
    }

    Continue reading...

Compartilhe esta Página