Environment
Describe the bug
Calling getRootView() returns undefined on both Android and iOS.
To Reproduce
import * as app from "tns-core-modules/application";
...
let rootView = app.getRootView();
Expected behavior
Expected a View object to be returned but it is always undefined. I have tried this on multiple machines and different platforms (iOS and Android).
Do u have code example on git or playground?
@Adam-Booth where is the method (let rootView = app.getRootView();) called ? Indeed, providing a Playground demo would be of great help to investigate your case.
Closing the issue due to not enough information - please follow the bug report template to provide the information needed.
My bad for not providing enough information.
I've pushed example code to a public repo (https://github.com/Adam-Booth/ns-test).
The login component is shown in the RadSideDrawer and is calling app.getRootView() in the ngAfterViewInit method.
My understanding is that when the login component is shown in the side drawer it is able to call getRootView in order to disable gestures. However, this isn't working.
Thanks
@Adam-Booth thank you for the provided project. The thing is that the Angular is an abstraction above the NativeScript (which is an abstraction above the native APIs). So that said, there are some cases where some of the native events won't be executed when the top abstraction (Angular) events are triggered. For example, with ngAfterViewInit you will be guaranteed that the Angular is rendered but that doesn't always mean that the native elements will be accessible.
In NativeScript most of those cases are covered but in your particular case (a drawer in the landing page), you might have to do a dirty hack and verify that the getRootView method is called on the next VM pass.
For example (as done here)
setTimeout(() => {
this.drawer = <RadSideDrawer>getRootView();
}, 100);
I guess that even a 1 ms timeout will do the trick.
Brilliant! Thanks @NickIliev, that got it working.
Cheers!
Most helpful comment
@Adam-Booth thank you for the provided project. The thing is that the Angular is an abstraction above the NativeScript (which is an abstraction above the native APIs). So that said, there are some cases where some of the native events won't be executed when the top abstraction (Angular) events are triggered. For example, with
ngAfterViewInityou will be guaranteed that the Angular is rendered but that doesn't always mean that the native elements will be accessible.In NativeScript most of those cases are covered but in your particular case (a drawer in the landing page), you might have to do a
dirtyhack and verify that thegetRootViewmethod is called on the next VM pass.For example (as done here)
I guess that even a 1 ms timeout will do the trick.