如何将带有ngFor循环的StackLayout转换为可滚动列表?

时间:2017-06-11 04:15:03

标签: nativescript angular2-nativescript

 <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>

我尝试将其打包在ScrollView中,但没有成功。

我无法使(items$ | async)ListView合作,似乎ngFor无法与ListView合作,我们需要ng-template ListView

我尝试了以下内容,但只有第一项是渲染

<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>

1 个答案:

答案 0 :(得分:3)

首先使用StackLayout然后使用ScrollView来处理整个代码

<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>