Search code examples
nativescriptnativescript-angular

NativeScript angular nativeView undefined


I am trying to get StackLayout nativeView from NativeScript Angular but always returning undefined. I tried like this:

html:

<StackLayout id="stackLayout" #stackLayout> </StackLayout>

TS:

ngAfterViewInit() {
    setTimeout(() => {
        let container: StackLayout = this.page.getViewById("stackLayout");
        console.log(container.nativeView); 
        console.log(this.stackLayout.nativeElement.nativeView);
    }, 100)
}

Please give me suggestion.


Solution

  • Use the loaded event of the View itself which ensures the nativeView is ready.

    HTML

    <StackLayout (loaded)="onLoaded($event)"></StackLayout>
    

    TS

    onLoaded(event: EventData) {
      const stackLayout = <StackLayout>event.object;
      console.log(stackLayout.nativeView);
    }