Angular 8 Tutorial

Angular 8 Introduction History and versions of Angular 8 Architecture of Angular 8 How to install Angular 8 and set-up it. Creating our first Angular 8 app Angular 8 app loading

Difference between Angular And react

Angular vs react

Angular 8 Advantages Disadvantage

Advantage and Disadvantage of Angular 8

Angular 8 file structure

Angular 8 file structure

Angular 8 components

Components of Angular 8

Angular 8 CLI Commands

All CLI commands of Angular

Angular 8 with Bootstrap

How to install bootstrap for Angular 8 Libraries of Angular 8

Angular 8 Routing

Routing in Angular 8

Angular 8 directives

Angular 8 Directives Angular 8 ngIf directive Angular 8 ngFor directive Angular 8 ngSwitch directive Angular 8 ngClass directive Angular 8 ngStyle directive

Angular 8 pipes

Angular 8 Pipes

Angular 8 databinding

Angular 8 Data binding Angular 8 Event binding Angular 8 Property binding Two-way data binding in Angular 8

String Interpolation In Angular 8

Angular 8 String interpolation

Angular 8 forms

Angular 8 Forms Data flow of forms in Angular 8 Creating forms in Angular 8 Testing and validation of forms in Angular 8

Error fixing in Angular 8

Error fixing in Angular 8

Dependency injection and services in Angular 8

Dependency injection services in Angular 8

Angular 8 Animations

Angular 8 Animations

Dynamic components in Angular 8

Dynamic components in Angular 8

Angular 8 Module

Angular 8 Module Deploying an angular 8 app

Introduction of unit testing in angular 8

Unit testing in angular 8

Observables in angular 8

Observables in angular 8

Angular 8 universal

Angular 8 universal

Angular 8 Changes and new features

New features and changes in Angular 8

Conclusion

Angular 8 Conclusion

Routing in Angular 8

Routing

Angular Router is a powerful JavaScript router is built and maintained by the Angular core team that can install from the package @angular/router. Routing provides a complete routing library with the possibility of multiple router outlets, different path strategies.

The angular Router is the main part of Angular platform. It allows developers to build single-page applications with multiple views and allow navigation between views.

Angular supports SPA using routing module ngRoute. The routing module acts based on the URL. It works as a browser navigation's bar, and it was navigating between the pages.

  • Enter URL in the address bar, and then the browser will navigate to the corresponding page.
  • Click on the link to the page and the browser will navigate to the new page.
  • Click on the browser on the back or forward, and the browser will navigate backward or forward according to the history pages.

Working of Router:

Whenever a user acts, such as clicking a link which loads a new page in the browser, the Router intercepts the browser behavior, and show and hide the view hierarchy.

If the Router finds the current application, it requires particular functionality, and it can be lazy to load the module on demand.

We can navigate to new views when the user clicks a button or select from a dropbox or in response to some other stimulus from any source. The router logs in the browser history so back and forward button works well.

To define navigation rules, we associate navigation path with our component. A path uses a URL- like a syntax that integrates our program data in the same way that template syntax integrates. We can apply program logic to choose views to show or hide, in response to user input and our access rules.

How to Set-up routing in Angular 8?

Routing could be easily added to a project. We were prompt if we have the message, " Would you like to add angular routing?”(Y/N),if we answered by Y. The angular Router was set up in our project without having added it manually.

Otherwise, if we have no option like that, then we have to import it manually in our project.

  • Firstly open our project where we have to import or add the routing module.
  • After that, for creating the module we have to write the command ng g c home –spec=false.
  • In the same directory, we have to write the command ng g module app-routing

According to the following screenshot.

How to Set-up routing in Angular 8
How to Set-up routing in Angular  step 2
How to Set-up routing in Angular  step 3

Angular brings many improved modules to the Angular ecosystem, including a new router called the component router. The component router is highly configurable and features packed Router. Features included are standard view routing, nested child routes, named routes, and route parameters. The concept related to the Router are:

Angular ecosystem

The Router-outlet

The Router-outlet is a directive accessible from the router library where the Router inserts the component and gets matched based on the current browser's URL. We can add multiple outlets in our angular application, which enables us to implement advanced routing scenarios.

Routes And Paths

Routes are definitions comprised of a path and component attributes. The path mention to the part of the URL that determines a unique view that can be displayed, and refer to the Angular component that needs to be associated with a path.

In a component, each route maps a URL path.

The path could take a wildcard string (**). The Router selects this route if the called URL doesn't match the explained routes. It can be used for displaying a "Not Found" view or redirecting to a specific view if there is no match.

Example:

{path:’contacts’, component: ContactListComponent}

If the route definition is provided to the router configuration, the router will render ContactListComponent when the browser URL for the web application /contacts.

Route Params

To creating the routes with parameters is a common feature in web apps. The angular Router allows us to access parameters in different ways.

We can create a router parameter using the colon syntax.

{path:‘contacts/:id’, component: contactDetailcomponent}

Route Guard

A route guard is the feature of the Angular Router which allows the developer to run logic if a router is requested, and it is based on the logic. It allows and denies the user access to the route. We can add a route guard by implementing the CanActivate interface available from @angular/router package. It can activate() method which holds the logic to allow and deny access to the route.

Example:

Class MyGuard implements CanActivate {
can activate () {
return true;
}
} 

Navigation Directive

The Angular Router provides the router link directive to create the navigation links. This directive generates the path associated with the component to navigate.

For example:

Contacts. 
Navigation Directive
Router Path