Angular2 Nativescript:如何轻松传递ListView项目

时间:2016-11-16 16:35:00

标签: listview angular nativescript

我有以下代码:

NotificationCompat.Builder notifBuilder =
            new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.alert_octagon_white)
                    .setContentTitle("contentTitle 2")
                    .setTicker("ticker 2")
                    .setContentText("contentText 2")
                    .setStyle(new NotificationCompat.BigTextStyle().bigText("bigText 2"));
NotificationManagerCompat.from(this).notify("tag2", 2, notifBuilder.build());

我想使用onTap方法对此列表项进行grub。这是onTap方法所在的组件:

  <ListView [items]="newsList" (itemTap)="onTap($event)" class="list-group">
    <template let-dataitem="item" let-i="index">
      <GridLayout class="list-group-item">
          <Label [text]="dataitem.title"></Label>
          <Label [text]="dataitem.body"></Label>
       </GridLayout>
    </template>
  </ListView>

在详细信息组件I&#39; m只是订阅queryParams

@Component({
   selector: "my-app",
   templateUrl: "app.component.html",
   styleUrls: ["app.component.css"],
   providers:[NewsService]
})

export class AppComponent {
  newsList;
  constructor(private router: Router, news: NewsService) {
     this.isLoading = true;
     news.getNews().subscribe(data => {
        this.newsList = data;
     })
  }

  public onTap(item: any) {
     this.isLoading = true;
     let navExtras: NavigationExtras = {
        queryParams:{
            "data" : item
        }
     }
     this.router.navigate(['/details'],navExtras);
  }
}

并显示数据..但是显示this.route.queryParams.subscribe(params => { this.data = params['data']; }); 。 我做错了什么,以及如何传递数据。

1 个答案:

答案 0 :(得分:0)

您在模板中声明了let-dataitem="item",因此在声明之后您需要收听点击事件:

<ListView [items]="newsList" class="list-group">
    <template let-dataitem="item" let-i="index">
      <GridLayout (tap)="onTap(dataitem)" class="list-group-item">
          <Label [text]="dataitem.title"></Label>
          <Label [text]="dataitem.body"></Label>
      </GridLayout>
   </template>
</ListView>