Skip to content

feat(compiler): Support instanceof operator in Built-in control flow (Angular templates) #59975

@sunio00000

Description

@sunio00000

Which @angular/* package(s) are relevant/related to the feature request?

compiler

Description

I'm interested in hearing opinions on allowing the use of the instanceof operator within the @if control block. Sometimes, I want to expose a template based on the instance of an object rather than its properties.

While Angular templates is designed with a declarative UI composition in mind, I wonder if instanceof might not be considered declarative enough. If there is any reason for not considering it, could it be related to performance or debugging concerns?

Example:

@if(myObject instanceof SomeClass) {
    <!-- This template will only be displayed if myObject is an instance of SomeClass -->
    <div class='some'>
         Content for SomeClass instance.
    </div>
}

Proposed solution

Here is a stackblitz

Alternatives considered

Pre-Computed Properties: Compute the instanceof result in the component and bind it to the template.
Custom Pipe: Use a custom pipe to encapsulate the instanceof logic.
Custom Structural Directive: Create a directive to handle the instanceof check declaratively.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: compilerIssues related to `ngc`, Angular's template compiler

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions