Categories
Angular Answers

How to fix HTTP Angular HttpClient not sending the header?

Sometimes, we want to fix HTTP Angular HttpClient not sending the header.

In this article, we’ll look at how to fix HTTP Angular HttpClient not sending the header.

How to fix HTTP Angular HttpClient not sending the header?

To fix HTTP Angular HttpClient not sending the header, we create a header object.

For instance, we write

http.get(url, {
  headers: { header1: "value1", header2: "value2" },
});

to call http.get with the url we want to make the request to.

And we set the headers property to an object with the request header keys and values.

Conclusion

To fix HTTP Angular HttpClient not sending the header, we create a header object.

Categories
Angular Answers

How to detect a route change in Angular?

Sometimes, we want to detect a route change in Angular.

In this article, we’ll look at how to detect a route change in Angular.

How to detect a route change in Angular?

To detect a route change in Angular, we use router.events.subscribe.

For instance, we write

import { Component } from "@angular/core";
import {
  Router,
  Event,
  NavigationStart,
  NavigationEnd,
  NavigationError,
} from "@angular/router";

@Component({
  selector: "app-root",
  template: `<router-outlet></router-outlet>`,
})
export class AppComponent {
  constructor(private router: Router) {
    this.router.events.subscribe((event: Event) => {
      if (event instanceof NavigationStart) {
        // ...
      }

      if (event instanceof NavigationEnd) {
        // ...
      }

      if (event instanceof NavigationError) {
        // ...
        console.log(event.error);
      }
    });
  }
}

to call this.router.events.subscribe with a callback that listens for navigation events.

In it, we check if event is an instance of NavigationStart, which is emitted when navigation starts.

Also, we check if event is an instance of NavigationEnd, which is emitted when navigation ends.

And we check if event is an instance of NavigationError, which is emitted when there’s an error during navigation.

Conclusion

To detect a route change in Angular, we use router.events.subscribe.

Categories
Angular Answers

How to bind select element to object in Angular?

Sometimes, we want to bind select element to object in Angular.

In this article, we’ll look at how to bind select element to object in Angular.

How to bind select element to object in Angular?

To bind select element to object in Angular, we can assign our object to [ngValue].

For instance, we write

<select [(ngModel)]="selectedValue">
  <option *ngFor="let c of countries" [ngValue]="c">{{ c.name }}</option>
</select>

to bind our selected drop down value to selectedValue with [(ngModel)].

Then we render the options with *ngFor.

And we assign the value of each option to an object with

[ngValue]="c"

Then selectedValue would be the object selected from the drop down.

Conclusion

To bind select element to object in Angular, we can assign our object to [ngValue].

Categories
Angular Answers

How to trigger change detection manually in Angular?

Sometimes, we want to trigger change detection manually in Angular.

In this article, we’ll look at how to trigger change detection manually in Angular.

How to trigger change detection manually in Angular?

To trigger change detection manually in Angular, we can use ChangeDetectorRef .

For instance, we write

import { ChangeDetectorRef } from '@angular/core';

to import it.

Then in our component, we write

constructor(private ref: ChangeDetectorRef) { 
}

to inject the ref ChangeDetectorRef object into our component.

Then in our component method, we write

this.ref.markForCheck();

to manually trigger change detection.

Conclusion

To trigger change detection manually in Angular, we can use ChangeDetectorRef .

Categories
Angular Answers

How to select an element in a component template with Angular?

Sometimes, we want to select an element in a component template with Angular.

In this article, we’ll look at how to select an element in a component template with Angular.

How to select an element in a component template with Angular?

To select an element in a component template with Angular, we can assign an ref to the element in the template.

Then we can use ViewChild to access it.

For instance, we write

import { Component, ElementRef, ViewChild } from "@angular/core";

@Component({
  selector: "my-app",
  template: ` <input #inputEl value="hello" /> `,
  styleUrls: ["./app.component.css"],
})
export class AppComponent {
  @ViewChild("inputEl") inputEl: ElementRef;

  ngAfterViewInit() {
    console.log(this.inputEl);
  }
}

to assign the inputEl ref to the input element.

Then we access the element with

@ViewChild("inputEl") inputEl: ElementRef

in the template.

We call ViewChild with the ref name.

Then we use this.inputEl to get the element in our component method.

Conclusion

To select an element in a component template with Angular, we can assign an ref to the element in the template.