简体   繁体   English

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

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

I want to assign index to the content , in order to add infowindow to markers based on their index . 我想为content分配index ,以便根据标记的index向标记添加信息窗口。

console.log(storeData.length) will returns 4 rows of data. console.log(storeData.length)将返回4行数据。

Right now, both method returns me the same result, 4 infowindow overlay with each other. 现在,这两种方法都返回了相同的结果,彼此重叠了4个信息窗口。 I seem a lot of examples , however I do not know how to implement to my code. 我似乎有很多示例 ,但是我不知道如何实现我的代码。 Especially the var marker,i; 特别是var marker,i;

TS 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);

}    

What I tried 我尝试了什么

 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);
 });

 }

Hello why mixing html in the ts? 您好,为什么在ts中混合html? maybe you have a specific reason for doing this but I don't think this is the way you want to go with angular. 也许您有这样做的特定原因,但我不认为这是您想要采用角度的方式。 I would prefer this way by defining that html in a component with input parameters which can be reusable and testable. 我更喜欢这种方式,方法是在组件中使用可重用和可测试的输入参数定义html。 I am just putting the bare code. 我只是把裸代码。 if you need additional help, you can ask. 如果您需要其他帮助,可以询问。

create a component as follows: 创建一个组件,如下所示:

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() {
  }

}

The corresponding html is as follows: 相应的html如下:

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

then in your other component where you need to use the above component. 然后在您需要使用上述组件的其他组件中。 See the TS below //guess storedata is an array 参见下面的TS //猜测的storageata是一个数组

private storeData: infowindowData[];

your template can be follows. 您的模板可以如下。 You can decide what to do with the index. 您可以决定如何处理索引。 You can create an additional input parameter in the component and pass it there. 您可以在组件中创建一个附加的输入参数,并将其传递给该组件。 Any additional logic for that component can be done there as well in OnInit. 该组件的任何其他逻辑都可以在OnInit中完成。

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

Hope that helps. 希望能有所帮助。 Ashley 阿什利

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

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