Я думаю, это то, что вы ищете: я поместил стиль в app.component.html
, чтобы продемонстрировать его в основном. Вы можете подумать о том, чтобы иметь отдельный компонент для него.
An также произвел небольшой рефакторинг.
Ссылка для всплывающей подсказки: https://www.w3schools.com/css/css_tooltip.asp
app.component. html:
<style>
.tooltip {
position: relative;
display: inline-block;
border-bottom: 1px dotted black;
}
.tooltip .tooltip-content {
visibility: hidden;
width: 120px;
background-color: black;
color: #fff;
text-align: center;
border-radius: 6px;
padding: 5px 0;
/* Position the tooltip */
position: absolute;
z-index: 1;
}
.tooltip:hover .tooltip-content {
visibility: visible;
}
</style>
<div>
<table>
<tr *ngFor="let x of statusdata1;">
<td style="border:1px solid"><span>{{x.vehicle_number}}</span></td>
<td style="border:1px solid">
<div *ngIf="x.statusAvailable" class="tooltip">
{{x.statusUrls[0]}}
<span class="tooltip-content">
<span *ngFor="let status of x.statusUrls">
<img src="{{status}}" />
</span>
</span>
</div>
</td>
</tr>
</table>
</div>
app.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
imageSource: any;
statusdata1: any;
customData: any;
ngOnInit() {
/* First data */
const jsonData = [{ "vehicle_number": 1, "status": "red,green" },
{ "vehicle_number": 2, "status": "yellow,red" }];
this.statusdata1 = this.createCustom(jsonData);
}
createCustom(data) {
return data.map(row => {
const statusAvailable = typeof row.status === 'string';
const statusUrls = statusAvailable
? row.status.split(',').map(s => this.generateUrl(s))
: [];
return {
...row,
statusAvailable,
primaryStatusUrl: statusAvailable ? statusUrls[0] : undefined,
statusUrls
}
});
}
generateUrl(status) {
return `/app/animate/${status}.png`
}
}