After the "markerClick" event triggers in Angular2 SebmGoogleMapMarker, the view fails to update

I am dealing with an array structured like this:

    locations: marker[] = [
    {id: '1',  lat: 51.5239935252832,    lng:  5.137663903579778,   content: 'Kids Jungalow (5p)', iconUrl: 'img/marker.png'},
    {id: '2',  lat: 51.523853342911906,  lng:  5.1377765563584035,  content: 'Kids Jungalow (5p)', iconUrl: 'img/marker2.png'},
    {id: '3',  lat: 51.5237298485607,    lng:  5.137969675407476,   content: 'Kids Jungalow (5p)', iconUrl: 'img/marker2.png'},
    {id: '4',  lat: 51.52355628836575,   lng:  5.138066234932012,   content: 'Kids Jungalow (5p)', iconUrl: 'img/marker2.png'},
    {id: '5',  lat: 51.52340275379578,   lng:  5.138211074218816,   content: 'Kids Jungalow (5p)', iconUrl: 'img/marker2.png'},
    {id: '6',  lat: 51.523199152806626,  lng:  5.138382735595769,   content: 'Kids Jungalow (5p)', iconUrl: 'img/marker2.png' },
    {id: '7',  lat: 51.5229955509073,    lng:  5.138511481628484,   content: 'Kids Jungalow (5p)', iconUrl: 'img/marker2.png'},
    {id: '8',  lat: 51.52280529912936,   lng:  5.138543668136663,   content: 'Kids Jungalow (5p)', iconUrl: 'img/marker2.png'},
    {id: '9',  lat: 51.523596340777075,  lng:  5.138463201866216,   content: 'Kids Jungalow (5p)', iconUrl: 'img/marker2.png'},
    {id: '700',lat: 51.523372714362736,  lng:  5.1386992362595265,  content: 'Kids Jungalow (5p)', iconUrl: 'img/marker2.png'},
    {id: '101', lat: 51.52329594683302,  lng:  5.138838711128301,   content: 'Kids Jungalow Giraffe', iconUrl: 'img/marker2.png'}
];

Whenever I click on a marker, a section should open displaying the information of that particular marker.

Below is the HTML code for the google maps:

<sebm-google-map-marker *ngFor="let location of locations" (markerClick)="updateDiv(location)"></sebm-google-map-marker>

Here is the updateDiv() function:

updateDiv(location: Location) {
    this.selectedLocation = location;
}

Below is the markup code for the div where I intend to show the id of the selected location:

<div *ngIf="selectedLocation" class="result-number">{{ selectedLocation.id }}</div>

However, it doesn't seem to be functioning correctly!

Answer №1

If you're facing issues with updating the DOM in Angular, consider explicitly invoking the detectChanges method from the ChangeDetectorRef class:

constructor(private cdr:ChangeDetectorRef) {
}

(...)

updateDiv(location: Location) {
  this.selectedLocation = location;
  this.cdr.detectChanges();
}

Make sure that the updateDiv method is correctly receiving the location parameter and updating the selectedLocation variable in your TypeScript class.

If the above approach doesn't resolve the issue, you can utilize the NgZone service:

constructor(private ngZone:NgZone) {
}

(...)

updateDiv(location: Location) {
  this.ngZone.run(() => {
    this.selectedLocation = location;
  });
}

In cases where the processing is initiated from an external object (such as a Google Maps API) outside the Angular component's scope, additional adjustments may be required.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How can we dynamically render a component in React using an object?

Hey everyone, I'm facing an issue. I would like to render a list that includes a title and an icon, and I want to do it dynamically using the map method. Here is the object from the backend API (there are more than 2 :D) // icons are Material UI Ic ...

The ForbiddenError has struck again, this time wreaking havoc in the realms of Node.js, Express.js

I am currently adapting this GitHub sample application to utilize Express instead of KOA. However, I am encountering an Access Denied issue when the / route in Express attempts to load the index.html. What specific modifications are required in the code be ...

Running your Angular application on a Node server: Step-by-step guide

I am looking to deploy my application using express on a Node server. This is the content of my server.js file: var express = require('express'); var path = require('path'); var app = express(); app.get('/', (req, res) => ...

Working with nested arrays in TypeScript and how to push values onto them

I am facing some challenges with nested array behavior in TypeScript. I am looking for a way to define a single type that can handle arrays of unknown depth. Let me illustrate my issue: type PossiblyNested = Array<number> | Array<Array<number& ...

"Experiencing sluggish performance with VSCode while using TypeScript and Styled Components

My experience with vscode's type-checking is frustratingly slow, especially when I am using styled components. I have tried searching for a solution multiple times, but have only come across similar issues on GitHub. I attempted to read and understa ...

The unexpected behavior of rxjs withLatestFrom

import { of, Subject, interval } from 'rxjs'; import { tap, startWith, map, delay, publishReplay, publish, refCount, withLatestFrom, switchMap, take } from 'rxjs/operators'; const source$ = new Subject(); const res ...

Unable to execute function: Angular 7 Platform-Browser-Dynamic (intermediate value) share is not defined

Recently, I have been working on upgrading our Angular 5 build to version 7. After installing webpack 4, rxjs 6.3.3, and angular 7.0.3, along with taking care of dependencies, I managed to successfully compile the bundle. However, a puzzling error seems to ...

The parameter 'response' is assumed to be of type 'any' and is not explicitly declared

In my attempt to send data from an angular HTML page to MVC core and receive a response, I encountered an error when trying to use the subscribe method: Parameter 'res' implicitly has an 'any' type. Below is the code snippet: import ...

Select specific columns from an array using Typescript

I have a collection of objects and I'm looking for a way to empower the user to choose which attributes they want to import into the database. Is there a method to map and generate a separate array containing only the selected properties for insertion ...

Authentication with API Tokens in Rails 5

I am currently developing an API using rails 5 and I am looking to implement token authentication for consumption with angular 2 as the frontend. After installing devise, I found that most tutorials use devise_token_auth for this purpose. Can someone clari ...

Tips on how to effectively simulate a custom asynchronous React hook that incorporates the useRef() function in jest and react-testing-library for retrieving result.current in a Typescript

I am looking for guidance on testing a custom hook that includes a reference and how to effectively mock the useRef() function. Can anyone provide insight on this? const useCustomHook = ( ref: () => React.RefObject<Iref> ): { initializedRef: ...

Interface for dynamic objects in Typescript

I am currently using JavaScript to create an object and would like to include an interface for the data: JavaScript: const childGroups: Children = {}; childGroups.children = []; // Adding some data childGroups.children.push(children); Interface: ...

Encountering compilation errors with Angular project following installation of a package

I recently installed the LocalStorage Package and encountered the following message: npm notice created a lockfile as package-lock.json. You should commit this file. npm WARN @angular/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfem ...

Issue encountered when utilizing TypeORM within NestJS: Unable to import Entity Repository

Issue with EntityRepository Import Despite having @nestjs/typeorm installed, VS Code is not recognizing the decorator I need to use. Any suggestions on how to resolve this? ...

The string returned from the Controller is not recognized as a valid JSON object

When attempting to retrieve a string from a JSON response, I encounter an error: SyntaxError: Unexpected token c in JSON at position In the controller, a GUID is returned as a string from the database: [HttpPost("TransactionOrderId/{id}")] public asyn ...

Whenever I attempt to launch the application on my device, an error message stating "plugin_Not_Installed Exception" occurs

I encountered a plugin_Not_Installed Exception when running the app on my device. Here are the details of my Ionic setup: Ionic: Ionic CLI : 5.4.16 Ionic Framework : @ionic/angular 6.2.2 @angular-devkit/build-angular : 1 ...

Can you please explain the significance of classes <T> and <U> in Angular 2?

After diving into the Angular 2 TypeScript documentation, I have come across these two classes frequently. Can someone provide a detailed explanation of what they are? One example code snippet from the QueryList API documentation showcases: class QueryLi ...

`How to Merge Angular Route Parameters?`

In the Angular Material Docs application, path parameters are combined in the following manner: // Combine params from all of the path into a single object. this.params = combineLatest( this._route.pathFromRoot.map(route => route.params) ...

"Encountering a problem with the debounceTime operator in rxjs and HTTP requests while using the keyup

I have been working on implementing server-side search in Angular 7. I managed to find some code for implementation, but unfortunately it is not functioning as expected. The issue I am encountering is that when searching for a string, the code sends mult ...

Angular 10 and Typescript: Variables assigned within the change event become undefined

In my code, I initialize an Algolia input and set an onchange event to it. This initialization takes place in a service. algolia_data; random_var; this.http.post<any>('APIENDPOINT', formData).subscribe(data => { instance = places({ ...