Changing the marker color when hovering on corresponding element -> Angular 4

I'm trying to change the color of google marker dynamically when I hover on one of the element that is in different component. I'm still learning Angular and I tried using Observables but without any success.

I have main.view component that stores all the cards( I call them events). Here is the part of main-view.component.ts code:

constructor( private eventService: EventsDataService) {
      ngOnInit() {

I'm injecting the service that fetches the data for my cards from the server and use getEvents method to convert it to JSON data using Observable. Then I use 2 *ngFor directives in html template to iterate over this data and fill the cards and markers. Here is the code:


 <app-event-card *ngFor="let event of events; let i = index"></app-event-card>
  <aside class="mapContainer"  [class.fixed]="fixed">
      <agm-map [class.fixed]="fixed"  [latitude]= "latitude"  [longitude]="longitude" >
        <agm-marker *ngFor="let event of events; let i = index"
        [latitude]= "event.location.coordinates[0]"
        label="{{event.price +'AUD'}}">

Now I would like to change the color or any property of the marker when I hover on one of the cards that corresponds to the marker but I have no idea how can I connect the data for those both components.

I would appreciate any help or tips regarding this issue.


2 answers

  • answered 2018-04-14 15:12 JB Nizet

    I don't know how your components look like, but all you need is something like that:

    <app-event-card (mouseenter)="event.hovered = true" (mouseleave)="event.hovered = false" *ngFor="let event of events; let i = index"></app-event-card>
    <aside ...>
      <agm-map ...>
        <agm-marker *ngFor="let event of events; let i = index"
          [color]="event.hovered ? 'blue' : 'red'">


  • answered 2018-04-14 16:22 Deep 3015

    list of markers outside map

    <ul><li *ngFor="let data of markers" (mouseenter)="updateColor(" (mouseleave)="updateColorR(">{{}}</li></ul>


      *ngFor="let m of markers; let i = index"
      (markerClick)="clickedMarker(m.label, i)"
      (dragEnd)="markerDragEnd(m, $event)">



    stackblitz demo