ViewChild(MyDirective) results in undefined when MyDirective is declared in Sub-Module

This issue has been tracked since 2023-01-20.

Which @angular/* package(s) are the source of the bug?

core

Is this a regression?

No

Description

When my-directive is used in app-root and the directive is declared in a sub-module (library) the viewchild is undefined.

When declaring a directive in the same module where the app-root is declared everything works.

Please provide a link to a minimal reproduction of the bug

https://anjunar.github.io/angular-simplicity/dist/documentation-app
(The Console shows undefined and Xr)

https://github.com/anjunar/angular-simplicity/blob/master/projects/documentation-app/src/app/app.component.ts
(Here viewport and test as ViewChild, with console out)

https://github.com/anjunar/angular-simplicity/blob/master/projects/documentation-app/src/app/app.component.html
(the as-viewport and app-test at the end)

https://github.com/anjunar/angular-simplicity/blob/master/projects/angular-simplicity/src/lib/as-viewport/as-
as-viewport.directive.ts

https://github.com/anjunar/angular-simplicity/blob/master/projects/documentation-app/src/app/app-test/app-test.component.ts
app-app-test.directive.ts

Please provide the exception or error you saw

@ViewChild(MyDirective) directive! : MyDirective is undefined

Please provide the environment you discovered this bug in (run ng version)

Angular CLI: 15.1.1
Node: 19.4.0 (Unsupported)
Package Manager: npm 9.2.0
OS: win32 x64

Angular: 15.1.0
... animations, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, router

Package Version

@angular-devkit/architect 0.1501.1
@angular-devkit/build-angular 15.1.1
@angular-devkit/core 15.1.1
@angular-devkit/schematics 15.1.1
@angular/cli 15.1.1
@schematics/angular 15.1.1
ng-packagr 15.1.1
rxjs 7.8.0
typescript 4.9.4

Anything else?

pkozlowski-opensource wrote this answer on 2023-01-20

We would need a minimal reproduction scenario to progress on this issue report.

JoostK wrote this answer on 2023-01-20

Please include a reproduction, without one this issue won't be actionable.

JoostK wrote this answer on 2023-01-20

And @pkozlowski-opensource beat me to it :-)

anjunar wrote this answer on 2023-01-20

I did an test workspace with only a app-root and the directive and it works.

So maybe it is something wrong with my setup.

anjunar wrote this answer on 2023-01-20

I have found the mistake...

import {AsViewportDirective} from "../../../angular-simplicity/src/lib/as-viewport/as-viewport.directive";
import {WindowManagerService} from "../../../angular-simplicity/src/lib/window-manager.service";

it must be:

import {AsViewportDirective} from "angular-simplicity";
import {WindowManagerService} from "angular-simplicity";

anjunar wrote this answer on 2023-01-20

It would be nice when it was checked that the public api is used and not otherwise...

pkozlowski-opensource wrote this answer on 2023-02-08

Sounds like this got resolved on the user end.

More Details About Repo
Owner Name angular
Repo Name angular
Full Name angular/angular
Language TypeScript
Created Date 2014-09-18
Updated Date 2023-03-21
Star Count 87006
Watcher Count 3028
Fork Count 23170
Issue Count 1418

YOU MAY BE INTERESTED

Issue Title Created Date Updated Date