Showing posts from June, 2017

How to use the Angular Router

How to use the Angular Router Today we will see how to use the Angular Router. The router allows us to define routes which are transformed to urls which are then understood by the browser. Having routes allows us to create different categories and access points to our website. This post will be composed by 6 parts: Define routes Router outlet Special routes Data and ActivatedRoute Resolve guard CanXXX guards 1. Define routes To start, we need to import the RouteModule and the Routes type from the Angular router. import { RouterModule, Routes } from '@angular/router'; The routes are defined via constant and then injected into the router module using either forRoot or forChild . forRoot is used to define routes on the main module and forChild is used to define routes on the child modules For example we can define two routes which we add to the main module: const routes: Routes = [ { path: 'home', component: MainPageComponent, }, { pa

Reactive form with Angular

Reactive form with Angular I have been following the reactive movement for a long time now, especially from UI perspective from push design with websockets to in browser reactivity with WebSharper.UI.Next. In term of form design, I always felt like there was a lack. A form concept is simple, but it always escalates to an extremely over designed code with inline validation, server validation, incorrect validation at time, multisteps, asynchronous selection, etc… Something was missing, how the validation was done and how dirty it was to code a proper post back form. WebSharper was the real first innovation. It came out with a set of combinator that I explained in a previous blog post removing the “dirtiness” of form handling. It just felt right . And when things in programming start to feel right, it means you are on the right path. Since its evolution from 1.x.x, Angular have supports for reactive form with a form builder. Today I will explain the concept and how to use it. 1. C

How to declare a component in angular

How to declare a component in angular Last week I explained what was the NgModule in angular, we saw what were the parameters we could add and what those parameters could be composed of. Today I would like to talk about another main piece of Angular, the Component. We will see what argument it takes and how we can use it. 1. Parameters 2. Input/Ouput 3. Access child component 4. NgOnChange 1. Parameters Component in Angular derives from directives. Some of the most used properties are: providers which defines a injectable values or services scoped only by the component and its children (compared to injectable provided to the module which is application wide) selector which defines a CSS selector to use the component Here are some of the parameter used to defined a component: styles for inline style in the component template to define inline template For example here is a simple component: @Component({ selector: 'app-square', template: '<div>&l