Search code examples
nativescriptangular2-nativescript

How can I make a StackLayout with ngFor loop into scrollable list?


 <StackLayout width="100%" *ngFor="let item of (videos$ | async)">
     <CardView class="studentCard" margin="2" elevation="10" radius="1">
        <GridLayout rows="auto, auto, auto" columns="auto, auto, *">
            <Image [src]="item.snippet.thumbnails.high.url" stretch="aspectFill" colSpan="3" row="0"></Image>
            <Label [text]="item.snippet.channelTitle" textWrap="true" row="2" colSpan="1" ></Label>
            <Label [text]="item.snippet.title" textWrap="true" row="2" col="1" colSpan="2" >></Label>
        </GridLayout>
     </CardView>
 </StackLayout>

I tried to wrap it in ScrollView, didn't work.

I couldn't make (items$ | async) work with ListView either, seems ngFor can't work with ListView, we need ng-template for ListView.

I tried the following but only the first item is rendering

<ScrollView>
  <ListView [items]="videos$ | async" class="list-group">
    <ng-template let-item="item">
       <GridLayout  class="list-group-item">
         <Image [src]="item.snippet.thumbnails.high.url"></Image>
         <Label [text]="item.snippet.channelTitle" ></Label>
         <Label [text]="item.snippet.title">></Label>
       </GridLayout>
    </ng-template>
  </ListView>
</ScrollView>

Solution

  • Wraping the entire code first with StackLayout then with ScrollView worked

    <ScrollView>
      <StackLayout>
    
       <StackLayout width="100%" *ngFor="let item of (videos$ | async)">
           <CardView class="studentCard" margin="2" elevation="10" radius="1">
              <GridLayout rows="auto, auto, auto" columns="auto, auto, *">
                  <Image [src]="item.snippet.thumbnails.high.url" stretch="aspectFill" colSpan="3" row="0"></Image>
                  <Label [text]="item.snippet.channelTitle" textWrap="true" row="2" colSpan="1" ></Label>
                  <Label [text]="item.snippet.title" textWrap="true" row="2" col="1" colSpan="2" >></Label>
              </GridLayout>
           </CardView>
       </StackLayout>
    
      </StackLayout>
    </ScrollView>