Distinguish Angular Component instance from other types

Discussão em 'Angular' iniciado por craPkit, Outubro 11, 2024.

  1. craPkit

    craPkit Guest

    In Angular, how can I distinguish a Component ('@angular/core') from ... anything else?

    My function expects either an Angular Component or an HTML Element. I've tried checking for instanceof Component, but that won't work, since it's decorated instead of extending Component.

    Ideally, I'd expect some framework-provided check, but I couldn't find one. What's the canonical way to check this? The following work-around is very kludgy.

    function isComponent(subject: Component | HTMLElement) {
    // return subject instanceof Component; // always false
    return !!(subject.constructor as any).ɵcmp; // works, but uses Angular-internals and anyScript
    }

    Continue reading...

Compartilhe esta Página