Accessing childviews/widgets/controls


#1

How do I access a child view component inside a layout component?

<ng-template *ngFor="loop...">

    <StackLayout class="item" (tap)="tapped($event)">
        <Label class="title" text="Hello"></Label>
    </StackLayout>

</ng-template>
tapped(args:EventData) {

    let stackLayout = <StackLayout>args.object;
    let label = ?

}

#2

Hey @johnnydoe, Hope this can help you!

You can access widget inside the layout as,

    <StackLayout class="item" (tap)="tapped($event)">
        <Label #hellolabel class="title" text="Hello"></Label>
    </StackLayout>
tapped(args:EventData) {
let hlabel= <Label>this.hellolabel.nativeElement;
hlabel.text= "Welcome";
}

Please have a look at Accessing NativeScript controls.


#3

Hi kumaran thank you for your reply.

If I have multiple StackLayout’s in a loop/list and I want to address the label in one of them trough the args variable, how do I do that?


#4

Found it

tapped(args:EventData) {

    let stackLayout = <StackLayout>args.object;
    let label = <Label>stackLayout.getChildAt(0);

}

#5

Have a look at this @johnnydoe,

 if (tabView.android) {
        var viewPager: android.support.v4.view.ViewPager = (<any>tabView)._viewPager;
        var androidListView = <android.widget.ListView>viewPager.getChildAt(0);
        var stackLayout = <org.nativescript.widgets.StackLayout>androidListView.getChildAt(0);
        var button = <android.widget.Button>stackLayout.getChildAt(0);
        button.performClick();
    }