Accessing the map in an Angular 6 service via Leaflet

Embedding a map into my Angular 6 app service has been a bit tricky. Currently, I'm passing it as an argument when calling an init function in the service and providing it via Subject from the component after fetching data from the store. However, sometimes I encounter an issue where I receive undefined instead of a map.

Here's a snippet of my code:

page.component.ts

export class PageComponent implements OnInit {

    map$ = new Subject<Map>();

    ngOnInit() {
        this.store.pipe(
            select(fromStore.getData)
        )
        .subscribe(data => {
            this.mapService.doSomething(this.map$);
        })
    }

    mapReady(map: Map) {
        this.map$.next(map);
    }
}

map.service.ts

export class MapService {
    map: Map;
    subscription$: Subscription;

    doSomething(map$: Observable<Map>) {
        this.subscription$ = map$.subscribe(data => {
            this.map = data;
        })
    }
}

Are there any alternative methods to pass a map into the service?

Answer №1

Have you considered accessing the map more directly instead of using unnecessary layers of indirection? Simplifying your code could be beneficial, like this:

map.service.ts:

export class MapService {
    map: Map = new Map();

    doSomething(map: Map) {
        this.map = map;
    }
}

page.component.ts:

export class PageComponent implements OnInit {

    ngOnInit() {
        this.store.pipe(
            select(fromStore.getData)
        )
        .subscribe(data => {
            this.mapService.doSomething(data);
        })
    }

    mapReady(map: Map) {
        this.mapService.doSomething(map);
    }
}

The types may need adjustments based on the functionality of this.store and fromStore, but it can be easily modified to fit your specific requirements.

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

ngClass is failing to be implemented

Does anyone know how to dynamically apply a CSS style to an input based on a variable value? HTML <input class="defaultInput" [ngClass]="{'inputerror':'emptyFields'}" formControlName="idAnnuaire" placeholder="Ex: c20011"> CSS ...

Refreshing the browser does not load the Angular2 component and instead shows a 404 error

During my exploration of Angular 2, I developed a basic restaurant management application. Now, I am delving into more advanced techniques such as creating an app bundle, minification, and optimizing the application. The authentication component in my app ...

Ending the Infinite Scroll in Ionic 3 When Data Runs Out

I am having an issue with my json data where I need to figure out how to stop the infinite scroll once there is no more data available. Can anyone help me implement this feature? Below is the code snippet for reference: handleDataLoad(currentCount) ...

Issue with Primeng p-chips component: unable to dynamically add objects

Passing an array of objects value1 to the p-chips component is causing some issues. app.component.ts import { Component } from '@angular/core'; import {MenuItem} from 'primeng/api'; @Component({ selector: 'app-root', tem ...

Is it feasible to update just one key within an exported type using setState in TypeScript?

Recently, I decided to dive into Typescript within my React App and encountered an error that has left me stumped. I'm wondering if it's possible to access a specific key in an exported type and modify its value? Here is how I've exported ...

Why isn't my Enum functioning properly to display the colored background?

Why isn't the Background Color showing up when I pass in the BGColor Prop dynamically in my next.js + Tailwind app? I have tried passing in the prop for my component, but the color is not appearing. <Title title='This is HOME' descripti ...

Leverage and implement a reusable class in Typescript

In a React Typescript project, I am facing a challenge. I want to utilize a base class component and add an additional property to its State. After attempting to modify the class from class ErrorBoundaryW extends PureComponent<any, State> {...} to ...

Struggling with Primeng's KeyFilter functionality?

I've implemented the KeyFilter Module of primeng in my project. Check out the code snippet below: <input type="text" pInputText [(ngModel)]="price.TintCost" [pKeyFilter]="patternDecimal" name="tintCost" required="true" /> Take a look at my Typ ...

Effortlessly transfer model data to a different component in Angular upon clicking a button

Component A displays a list of Products, each with an option to view the details. When this option is clicked, I want Component B to show a list of items associated with that specific Product. I am having trouble passing the selected Product from Componen ...

The Scrollable feature in Material2 cdk remains unutilized due to

Trying to implement the new APIs for listening to scroll events in Material2 has proven challenging. I followed the steps of importing the ScrollDispatchModule in my app.module.ts file and marking a container with the cdkScrollable directive: <div cdkS ...

Updating color of an element in SVG and Angular2+ according to the background

In my svg element, I have a text element positioned after two rect elements. <svg id="floor-plan" width="300" height="100"> <rect width="300" height="100" fill="white"/> <rect width="50" height="50" fill="green"/> <text x="10" y="10" ...

A guide on how to retrieve images from a URL and save them using Blob in Angular 5

In my web application, I have a few links that lead to files with different content types - some are application/pdf and others are image/jpeg. When clicking on these links, the file should download or save based on their respective type. While downloadin ...

Jasmine-ts is encountering a problem related to a subpath within a package, causing

Embarking on a journey of jasmine unit testing, I am encountering challenges when trying to run jasmine and locate my TypeScript written specs. Using jasmine-ts, I typically execute the following command: jasmine-ts --config=spec/support/jasmine.json The ...

Discover the power and ease of combining Angular with OIDC Implicit Flow for seamless

I have integrated the angular-auth-oidc-client package for authentication in my Angular application with our OIDC server. While using the implicit flow, some users face log out issues when the access token expires. To address this, I decided to implement t ...

What is the method for retrieving the second type of property from an array of objects?

I have a collection of objects that map other objects with unique identifiers (id) and names (name). My goal is to retrieve the name corresponding to a specific id. Here is my initial approach: const obj = { foo: { id: 1, name: 'one' }, ...

Is there a way to adjust the dimensions of my Angular npm slider?

This snippet shows the Angular code I came across this npm slider on npm.js Struggling to adjust the margin of the images as they are sticking, also unsure about modifying the size and other elements using https://www.npmjs.com/package/ng-image-slider. A ...

Guide on utilizing the useContext hook in React/Next.js while incorporating TypeScript

As I embark on my journey in the world of TypeScript, I find myself working on a new project in Next.js using TypeScript. My goal is to incorporate authentication functionality into this project by utilizing createContext. Coming from a background in JavaS ...

Establish a table containing rows derived from an object

I am currently dealing with a challenge in creating a table that contains an array of nested objects. The array I have follows this schema: array = [ { id: 'Column1', rows: { row1: 'A', row2 ...

Running a single test in Angular without using fdescribe or fit

My project has numerous tests that are not being maintained, causing errors when running ng test due to import issues in .spec.ts files. Is there a way to execute a single file test for a service without having to clean up all the tests? Perhaps using Php ...

Auth0 Angular - No routes found to match

I recently set up an angular application and integrated it with Auth0 by following two helpful tutorials: https://auth0.com/docs/quickstart/spa/angular2/01-login https://auth0.com/docs/quickstart/spa/angular2/02-calling-an-api Here is a brief overview o ...