When I declare a second service in the constructor, my modal service no longer opens as expected

Recently, I came across this login method in Angular:

email: string = "";
  password: string = "";
  login() {
    const credentials = {
      email: this.email,
      password: this.password
    };
    this.userService.login(credentials).subscribe(
      (response: any) => {
        sessionStorage.setItem('token', response.token);
        sessionStorage.setItem('email', response.email)
        this.router.navigate(['/dashboard']);
      },
      (error) => {
        console.error('Login failed:', error);
      }
    );
    this.modalService.dismissAll();
  }

This method is part of my own userService.

login(data: any): Observable<any> {
    return this.httpClient.post(this.url + "/login", data, {
      headers: new HttpHeaders().set('Content-Type', 'application/json')
    });
  }

However, every time I declare the userService in the main page constructor and implement the logic, my modal stops working.

constructor(private userService:UserService,private modalService:NgbModal,
              private router:Router) {
  }

I attempted consolidating the methods into one file to troubleshoot, but it still didn't resolve the issue. I am expecting the modal to open as intended, but it remains non-functional.

Answer №1

After some troubleshooting, I discovered the issue was due to my oversight of not including HttpClientModule in app.module.ts. This became apparent when I encountered the following error message in the console:

ERROR NullInjectorError: R3InjectorError[_UserService -> _UserService -> _HttpClient -> _HttpClient]: 
  NullInjectorError: No provider for _HttpClient!

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

typescript scrolling location

In my Angular UI code, I have a component class that includes the following structure: app.component.html //... <div class="banner"> <p-dialog [(visible)]="displayCOI" styleClass="coiDialog" [contentStyle]=" ...

Encountered a syscall spawn git error while running npm install command

I recently attempted to execute npm install and encountered the following problems: Even after attempting to clear cache forcibly, installing git, and updating node, none of these solutions proved effective. Above is the specific error message I received ...

`How can I extract HTMLElements from slots in vue3?`

When attempting to develop a Layer component, I encountered some challenges. Here is the code: // Wrapper.vue <template> <slot v-bind="attrs"></slot> </template> <script lang="ts" setup> import { defi ...

What is the process for moving information between files?

I have two files which are named as, employee-rates-controller.ts: private load() { return this.entityService .load(this.$scope.projectRevisionUid) .then(resp => { localStorage.removeItem('employeerates'); this.$ ...

By default, Angular 2's routerLinkActive is set to active for dynamic routes

Within my ngOnInit function, I am dynamically obtaining two links and URLs. <li><a [routerLink]="welcomeUrl" routerLinkActive="active" [routerLinkActiveOptions]="{ exact: true }">Getting Started</a></li> <li><a [routerLin ...

Why isn't my Promise fulfilling its purpose?

Having trouble with promises, I believe I grasp the concept but it's not functioning as expected in my project. Here is a snippet of my code : (I am working with TypeScript using Angular 2 and Ionic 2) ngOnInit() { Promise.resolve(this.loadStatut ...

The confirm alert from Material UI is being obscured by the dialog

How can I ensure that a material ui dialog does not hide the alert behind it when confirming an action? Is there a way to adjust the z index of the alert so that it appears in front of the dialog? import Dialog from "@material-ui/core/Dialog"; i ...

Binding user input to a preset value in Angular with data binding

My goal is to have a predefined input form with the email provider value preset. However, when I try to submit the form without modifying it, it does not upload anything to Firebase unless I manually delete the value and re-enter it. <input class="butt ...

Tips for utilizing functions in an inline HTML translation pipe

My objective is to streamline the code by using the Angular translate pipe. Currently, I find myself using 8 curly brackets and repeating the word "translate" twice... there must be a more efficient approach. Here is my current code setup: <s ...

Angular seat map for booking a flight

I'm stuck and in need of assistance. I am trying to change the color of a seat (add or remove a class) in Angular, but I can't figure out how to do it. Here is my current solution using JS: https://codepen.io/mateuszcieslik/pen/mdpLqgw. I have a ...

Accessing attributes of a parent class object from within a child object

Imagine having four tabs within an Angular component, each with its own set of criteria for being displayed. Here's a high-level overview of the scenario. export class DisplayTabs { foo: true; bar: false; tabs: { 'A': { order: 1, g ...

Using default parameters in a versatile function

There is a function called zip with the following signature: function zip<T, U, V>(ts: T[], us: U[], zipper: (t: T, u: U) => V): V[] An attempt is made to assign a default value of (t, u) => [t, u] to the zipper argument: function zip<T, ...

Exploring the differences in two date variables using Angular

Is it possible to compare these two dates directly or should I convert one of them first? date1: 2019-07-31T23:00:00 date2: Fri Aug 30 2019 00:00:00 GMT+0100 (West Africa Standard Time) I am using Angular for this task. ...

Can a TypeScript generic version of the Y-combinator be successfully executed?

Here is an interesting JavaScript implementation of the Y-combinator: const Y = g => (x => g(x(x)))(x => g(x(x))) //or const Y = f => { const g = x => f(x(x)) return g(g) } I've been thinking, could it be possible to create a TypeS ...

Ways to trigger an event based on the outcome of a pop-up window

In my current project, I am working on a component that includes three buttons: two default options and an additional modal dialog option for more advanced choices. When one of the default buttons is clicked, the component emits an event based on the selec ...

Steps for undoing the impact of the Bootstrap 5 'text-muted' styling

I am currently utilizing bootstrap 5 to apply the "text-muted" class, which greys out or dims the text. However, I would like the text to return to its normal state when hovered over. While I have managed to create other hover effects such as underlining t ...

Mastering the use of SVG icons in Angular 4+: A comprehensive guide

I have an icon.svg file with a collection of icons that I would like to use in my app, similar to how we display material icons. Does anyone have any ideas on how to include the file in the app and use these icons? I've tried looking for solutions a ...

TypeScript overload does not take into account the second choice

Here is the method signature I am working with: class CustomClass<T> { sanitize (value: unknown): ReturnType<T> sanitize (value: unknown[]): ReturnType<T>[] sanitize (value: unknown | unknown[]): ReturnType<T> | ReturnType< ...

Issue arises from having multiple instances of CKEditor5 when transferring information to a Modal

Utilizing AJAX requests to fetch data from the database has been helpful, except for one issue: when passing the data into CKEditor5, I encounter an error. Each time I close and reopen the modal, a new instance of the modal is displayed even though the dat ...

Ensure that the dynamically inserted <title> tag remains intact in Angular even when the page is re

Can the dynamic title tag be preserved when the page is refreshed? When I refresh the page, the title tag reverts back to the original one specified in the index.html temporarily before switching back to the dynamically added one. I want the title tag to ...