Categories
Angular Answers

How to determine previous page URL in Angular?

Sometimes, we want to determine previous page URL in Angular.

In this article, we’ll look at how to determine previous page URL in Angular.

How to determine previous page URL in Angular?

To determine previous page URL in Angular, we can listen for navigation events.

For instance, we write

import { filter, pairwise } from "rxjs/operators";

to import the filter and pairwise operator functions.

Then we write

this.router.events
  .pipe(
    filter((evt: any) => evt instanceof RoutesRecognized),
    pairwise()
  )
  .subscribe((events: RoutesRecognized[]) => {
    console.log("previous url", events[0].urlAfterRedirects);
    console.log("current url", events[1].urlAfterRedirects);
  });

to call this.router.events.pipe to return events that are instance of the RoutesRecognized class.

We call pairwise to return the current and previous navigation events.

Then we call subscribe with a callback to get both events’ objects and get the URL with urlAfterRedirects.

Conclusion

To determine previous page URL in Angular, we can listen for navigation events.

Categories
Angular Answers

How to pass URL query string to a HTTP request on Angular?

Sometimes, we want to pass URL query string to a HTTP request on Angular.

In this article, we’ll look at how to pass URL query string to a HTTP request on Angular.

How to pass URL query string to a HTTP request on Angular?

To pass URL query string to a HTTP request on Angular, we use httpClient.

For instance, we write

const data = { limit: "2" };
this.httpClient.get<any>(apiUrl, { params: data });

to call httpClient.get with the apiUrl we want to make the request to.

Then 2nd argument is an object wth the params property set to data.

The data key-value pairs will be automatically appended to the URL as query string parameters.

Conclusion

To pass URL query string to a HTTP request on Angular, we use httpClient.

Categories
Angular Answers

How to use jQuery Plugin with Angular?

Sometimes, we want to use jQuery Plugin with Angular.

In this article, we’ll look at how to use jQuery Plugin with Angular.

How to use jQuery Plugin with Angular?

To use jQuery Plugin with Angular, we can install the jquery package with its TypeScript type definitions.

To install them, we run

npm install jquery
npm install -D @types/jquery

Then we use it by writing

import * as $ from "jquery";

//...
export class JqueryComponent implements OnInit {
  constructor() {}

  ngOnInit() {
    $(window).click(() => {
      alert("ok");
    });
  }
}

to import jquery with

import * as $ from "jquery";

Then we use it with

$(window).click(() => {
  alert("ok");
});

Conclusion

To use jQuery Plugin with Angular, we can install the jquery package with its TypeScript type definitions.

Categories
Angular Answers

How to set base href dynamically with Angular?

Sometimes, we want to set base href dynamically with Angular.

In this article, we’ll look at how to set base href dynamically with Angular.

How to set base href dynamically with Angular?

To set base href dynamically with Angular, we can add an entry to the providers array in NgModule.

For instance, we write

import { APP_BASE_HREF } from "@angular/common";
import { NgModule } from "@angular/core";

@NgModule({
  providers: [
    {
      provide: APP_BASE_HREF,
      useValue: "/" + (window.location.pathname.split("/")[1] || ""),
    },
  ],
})
export class AppModule {}

to call @NgModule with an object that has the providers property.

In the providers array, we add an entry to set the base element’s href attribute dynamically with

{
   provide: APP_BASE_HREF,
   useValue: "/" + (window.location.pathname.split("/")[1] || ""),
}

We set the base element’s href attribute to the value of useVale.

Conclusion

To set base href dynamically with Angular, we can add an entry to the providers array in NgModule.

Categories
Python Answers

How to import Lodash into an Angular TypeScript application?

Sometimes, we want to import Lodash into an Angular TypeScript application.

In this article, we’ll look at how to import Lodash into an Angular TypeScript application.

How to import Lodash into an Angular TypeScript application?

To import Lodash into an Angular TypeScript application, we can install the lodash package.

To install it, we run

npm install --save lodash
npm install --save-dev @types/lodash

to install the lodash package and the TypeScript type definitions for the lodash package respectively.

Then we import it by adding

import _ from "lodash";

in our Angular app’s code.

Conclusion

To import Lodash into an Angular TypeScript application, we can install the lodash package.