繁体   English   中英

角2 /离子2-用于ts中的循环

[英]angular 2/ionic 2 - for loop in ts

我想为content分配index ,以便根据标记的index向标记添加信息窗口。

console.log(storeData.length)将返回4行数据。

现在,这两种方法都返回了相同的结果,彼此重叠了4个信息窗口。 我似乎有很多示例 ,但是我不知道如何实现我的代码。 特别是var marker,i;

TS

for ( let infowindowData of storeData){

  let content = '<ion-item>' + 
     '<h2 style="color:red;">' + infowindowData.ListingTitle +'</h2>' + 
     '<p>' + infowindowData.ListingDatePosted + ' ' + infowindowData.ListingTime + '</p>' +
     '</ion-item>';    

     this.addInfoWindow(marker, content);

}    

我尝试了什么

 let storeData =  this.data;

 for(let i=0; i<storeData.length;i++){

 let content = '<ion-item>' + 
     '<h2 style="color:red;">' + storeData[i].ListingTitle +'</h2>' + 
     '<p>' + storeData[i].ListingDatePosted  + ' ' + storeData[i].ListingTime  + '</p>' +
     '<p> Salary: $' + storeData[i].ListingSalary  + '</p>' +
     '</ion-item>';    


 let infoWindow = new google.maps.InfoWindow({
 content: content
 });

 google.maps.event.addListener(marker, 'click', () => {
 infoWindow.open(this.map, marker);
 });

 }

您好,为什么在ts中混合html? 也许您有这样做的特定原因,但我不认为这是您想要采用角度的方式。 我更喜欢这种方式,方法是在组件中使用可重用和可测试的输入参数定义html。 我只是把裸代码。 如果您需要其他帮助,可以询问。

创建一个组件,如下所示:

import { Component, OnInit, Input } from '@angular/core';

@Component({
  selector: 'info-window',
  templateUrl: './component.html',
  styleUrls: ['./component.css']
})
export class InfoWindowComponent implements OnInit {

  @Input('title') listingTitle :string;
  @Input('date') datePosted :string;
  @Input('time') listingTime :string;
  constructor() { }

  ngOnInit() {
  }

}

相应的html如下:

<ion-item> 
     <h2 style="color:red;">{{listingTitle}}</h2> 
     <p>{{datePosted}} {{listingTime}}</p>
</ion-item>

然后在您需要使用上述组件的其他组件中。 参见下面的TS //猜测的storageata是一个数组

private storeData: infowindowData[];

您的模板可以如下。 您可以决定如何处理索引。 您可以在组件中创建一个附加的输入参数,并将其传递给该组件。 该组件的任何其他逻辑都可以在OnInit中完成。

<li *ngFor="let infoWindow of storeData; let index = index">
  <info-window [title]=infoWindow.ListingTitle
[date]=infoWindow.ListingDatePosted [time]=infoWindow.ListingTime></info-window>
</li>

希望能有所帮助。 阿什利

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM