Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug(all): directives are not standalone #26036

Closed
1 task
ghost opened this issue Nov 20, 2022 · 18 comments
Closed
1 task

bug(all): directives are not standalone #26036

ghost opened this issue Nov 20, 2022 · 18 comments
Labels
needs investigation A member of the team needs to do further investigation to determine the root cause P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent

Comments

@ghost
Copy link

ghost commented Nov 20, 2022

Is this a regression?

  • Yes, this behavior used to work in the previous version

The previous version in which this bug was not present was

No response

Description

As the standalone APIs have become stable in v15, I expected @angular/material directives to be declared as standalone, but it did not happen for some reason?

Reproduction

import { MatTooltip } from '@angular/material/tooltip';

@Directive({
  selector: '[appCustomTooltip]',
  hostDirectives: [MatTooltip],
})
export class CustomTooltipDirective {}

Expected Behavior

MatTooltip and MatLegacyTooltip should be standalone directives

Actual Behavior

Error: NG0308: Host directive MatTooltip must be standalone.

Environment

Angular CLI: 15.0.0
Node: 18.12.0
Package Manager: npm 9.1.1
OS: win32 x64

Angular: 15.0.0
... animations, cdk, cli, common, compiler, compiler-cli, core
... forms, material, platform-browser, platform-browser-dynamic
... router

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1500.0
@angular-devkit/build-angular   15.0.0
@angular-devkit/core            15.0.0
@angular-devkit/schematics      15.0.0
@schematics/angular             15.0.0
rxjs                            7.5.7
typescript                      4.8.4
@ghost ghost added the needs triage This issue needs to be triaged by the team label Nov 20, 2022
@maartentibau
Copy link

It's a work in progress I think.
At this moment only CdkMenu and the sub-elements are standalone from what I can make of the source code.

Maybe it might be an idea to clearly state in the docs which components are already standalone and which ain't.

@terameta
Copy link

I agree with @maartentibau about a list and hopefully a roadmap for the standalone directives.
Currently I am also having the same issue with CDK Overlay, which will probably open many possibilities for most of the developers who are consuming CDK components/directives.

@wagnermaciel wagnermaciel added needs: discussion Further discussion with the team is needed before proceeding and removed needs triage This issue needs to be triaged by the team labels Apr 3, 2023
@wagnermaciel
Copy link
Contributor

Hi, this is something that's on our radar but we still haven't been able to fully plan out. It's definitely something on the horizon, though

@wagnermaciel wagnermaciel added P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent needs investigation A member of the team needs to do further investigation to determine the root cause and removed needs: discussion Further discussion with the team is needed before proceeding labels Apr 5, 2023
@michaelfaith
Copy link

@wagnermaciel if you're inviting external contributions for the standalone migration work, I'd be up for helping with it.

@andrewseguin
Copy link
Contributor

andrewseguin commented Jun 12, 2023

Hey @michaelfaith - this work is pending some team discussion about our strategy for providing components that have a lot of dependencies. Previously, these would all be bundled in the NgModule, but as standalone we need to decide how users should consume them.

This is something we want to do, but likely after v17

@hakimio
Copy link

hakimio commented Jun 12, 2023

If you would fix angular/angular#48089, you could just export read-only array like the documentation suggests.

@meriturva
Copy link
Contributor

What I'm expecting is to use a few directives (cdkTrapFocus) has composition: https://angular.io/guide/directive-composition-api#adding-directives-to-a-component

I advise starting with the incremental adoption of "standalone" only for cdk directives with few dependencies (the same approach we are taking here on our private component library).

My two cents.

@hakimio
Copy link

hakimio commented Jun 13, 2023

If you are unable/unwilling to merge angular/angular#48106 for whatever reason (some fragile internal tests failing nobody wants to touch?) then the only other option is to use NgModule:

const COMPONENTS = [MyStandaloneDirective, MyStandaloneComponent];

@NgModule({
    imports: COMPONENTS,
    exports: COMPONENTS
})
export class StandaloneComponentModule {}

@meriturva
Copy link
Contributor

Ok @hakimio but the main issue here is:

https://angular.io/guide/directive-composition-api#adding-directives-to-a-component

Specially:

Directives used in hostDirectives must be standalone: true.

@hakimio
Copy link

hakimio commented Jun 13, 2023

Yes, I know that you can't use non-standalone directives as host-directives, my comment was meant for @andrewseguin

@michaelfaith
Copy link

Hey @michaelfaith - this work is pending some team discussion about our strategy for providing components that have a lot of dependencies. Previously, these would all be bundled in the NgModule, but as standalone we need to decide how users should consume them.

This is something we want to do, but likely after v17

@andrewseguin Sounds good. At the point where you're ready to move on it, if you're inviting contributions, I'd be up for helping.

@flensrocker
Copy link

MatSuffix is something I want to use as host directive and stumbled upon this issue...

Without this, every user of my reusable suffix component has to remember to put that on the element. Not that bad but it would be nice to "DRY" that out...

@liesahead
Copy link

CdkMonitorElementFocus also can't be used that way, sadly.

@roma2341
Copy link

roma2341 commented Sep 14, 2023

Please answer me why custom standalone directives can't import modules ? I need to show MyModalComponent. But it has it's own MyModalModule where I import material components. If I try to show MyModalComponent from my directive as it is it will throw me a bunch of errors e.g mat-icon is not defined, because my directive doesn't know anything about MyModalModule... i don't understand this, angular team just make feature and don't describe how should we use their standalone directives without ability to import things we need. When I asked this question here my ticked was closed after 15 minutes, I tried to reproduce my issue, but due to peculiarities of angular providers angular team considered that I'm an idiot who makes simple errors and didn't help me
I'm using hostDirectives to reduce code duplicity so I can't use old modules where I could import what I need because of error: Directives used in hostDirectives must be standalone: true.

@michaelfaith
Copy link

If MyModalComponent isn't standalone too, then you should be importing MyModalModule into your directive's imports, rather than MyModalComponent.

@roma2341
Copy link

roma2341 commented Sep 14, 2023

If MyModalComponent isn't standalone too, then you should be importing MyModalModule into your directive's imports, rather than MyModalComponent.

The problem is that Directives doesn't have imports. I understand that If i want to show standalone component it's ok, but if my component is not standalone i wouldn't be able to show it in modal from my Directive. because i need to import module with it somewhere, or i will have to import MyModalModule in every component that defined hostDirectives with my directive, which shows my modal

@michaelfaith
Copy link

Right, sorry. Brain hasn't gotten up to full speed yet.

@JeanMeche
Copy link
Member

CDK & Material were ported to standalone by #28155.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs investigation A member of the team needs to do further investigation to determine the root cause P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent
Projects
None yet
Development

No branches or pull requests