Using hostDirectives inside Directives easily leads to error NG0309

This issue has been created since 2022-11-19.

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

core

Description

By using the shiny new hostDirectives API, I've found myself easily running into error NG0309.

NG0309: Directive MyDirective matches multiple times on the same element. Directives can only match an element once.

In my case I am using a DestroyDirective (for handling rxjs subscriptions).
Sample repository: https://github.com/TomTomB/multiple-same-host-directive-issue

In it is a component with a template, that uses 2 directives on the same element (app-a and app-b)

<div app-a app-b></div>

Both directives have the following logic:

@Directive({
  selector: '[app-a]',
  standalone: true,
  hostDirectives: [DestroyDirective],
})
export class ADirective {
  private readonly _destroy$ = inject(DestroyDirective).destroy$;
}

This results in the error mentioned above.
The error is, of cause, not only specific to this exact use case. I fact, it's a pretty big road block when thinking the new "composition way" of reusing code.
Also, the error is really not that helpful for explaining, why this code is not working.
This could especially be true when using 3rd party packages that use hostDirectives on their own.

Proposed solution

Using hostDirecitves inside a directive should never result in a NG0309 error.

Alternatives considered

Removing hostDirecitves from the Directive decorator - but that would be a big bummer imho.

dylhunn wrote this answer on 2022-11-21

cc @crisbeto -- just for visibility

More Details About Repo
Owner Name angular
Repo Name angular
Full Name angular/angular
Language TypeScript
Created Date 2014-09-18
Updated Date 2022-12-01
Star Count 85220
Watcher Count 3042
Fork Count 22607
Issue Count 1316

YOU MAY BE INTERESTED

Issue Title Created Date Comment Count Updated Date
Docker image can run on multiple cpu platforms 0 2022-07-25 2022-10-08
Error when creating WMI service sensor c# 3 2021-11-12 2022-10-03
[Remove Request] - Blocking Amazon Cognito - cognito-identity.us-east-1.amazonaws.com 2 2021-08-02 2022-10-08
Send Alarms via Matrix-Server 0 2022-05-24 2022-09-27
[question] LED Strip 12 2021-08-13 2022-05-24
Network Error: Device Unreachable 5 2021-08-23 2022-05-24
Updated Linux install instructions 2 2021-03-23 2022-11-01
Error when enabling glonass,galieo or beidou. ( Zed-f9p) 4 2021-11-05 2022-10-11
CAN'T START BOT PLEASE HELP 2 2021-08-16 2022-09-27
Tab on a selected component displays 2 values 3 2022-08-11 2022-11-17
found error in network ERR_NAME_NOT_RESOLVED 1 2021-06-09 2022-10-03
ERC223 interface and implementation not fully compatible with EIP20 8 2018-05-31 2022-09-27
Interceptor issue in 4.5.5 2 2021-11-26 2022-09-14
[EN UPDATE] Client:6.0.01 Data:21-12-09-08-44-47-f3c07f 0 2021-12-18 2022-09-27
Internet connection failed... 17 2021-10-08 2022-10-24
Meta: export header value 0 2021-10-26 2022-10-23
feature_request: db中提供基于shortcut的索引 0 2021-09-13 2022-11-12
How about a function toBool? 2 2020-08-22 2022-10-30
Change getDefault behaviour 2 2020-07-08 2022-11-18
[feature request] add shortcut method to get by primary key 1 2022-05-19 2022-11-21
support attribute declaration 2 2020-03-15 2022-11-22
Outdated `analyzer` dependency on ferry_generator 5 2021-10-05 2022-11-17
add adjuster to process simulation 1 2019-08-14 2022-10-27
Fix issue normal liquid density of components 2 2021-01-26 2022-10-03
Add GERG-2008 EoS 2 2019-06-05 2021-12-26
I can not install It can not find the serve 2 2022-08-08 2022-10-04
[APIM] Subscription created without API Key 2 2022-03-18 2022-11-12
Failed to delete one APIBInding tenancy.kcp.dev object 0 2022-08-16 2022-09-20
Parcel serve fails with source maps 2 2022-04-13 2022-10-17
Web AppLoading is blank 2 2021-11-23 2022-11-10
"screenWakeupCommand" dosent do anything 5 2021-10-10 2022-11-20
[BUG] Ignore current id for backend unique validation 2 2021-04-01 2022-11-27
Add support for import assertions syntax 0 2021-12-14 2022-11-24
pgm file judging condiction 0 2021-03-31 2022-11-19
Fluentbit failed to send logs to elasticsearch ( Failed to flush chunk) 4 2021-11-17 2022-11-18
a question about Centrality Encoding 2 2021-08-18 2022-10-12
create_list with different values for the same attribute 9 2015-06-06 2022-12-01
Add builder for LSP code actions 4 2022-07-14 2022-10-06
Integration Test is failing on master branch 13 2022-03-04 2022-09-21
关系抽取 (RE)模型单独推理时数据输入的疑问 1 2022-04-13 2022-09-22
File descriptor leak in Vulnerability Detector 0 2021-12-02 2022-11-27
Question: Why not retrain on full dataset if eval_method = cv? 2 2021-07-08 2022-11-03
Purge <nixpkgs>, pass nixpkgs as argument from the repository_rule instead 0 2020-11-23 2022-10-11
nmp' is not recognized as an internal or external command, operable program or batch file. 1 2022-03-31 2022-11-29
Use `coingeckoId` to fetch prices whenever possible 1 2022-01-19 2022-11-14
REQUEST: Publicize Release Cadence Survey 2 2022-03-09 2022-11-21
[Maps] Add to troubleshoot section details on cleaning data before uploading 1 2022-06-28 2022-10-11
[Bug Report][2.6.7] Adding an icon to a v-select does not render properly. 1 2022-07-07 2022-10-26
Horizontal scrollbar missing using filter that does not return any results 0 2018-03-18 2022-11-27
[tool_crash] FileSystemException: Failed to decode data using encoding 'utf-8', null 2 2022-06-23 2022-11-28