How to use the Angular Router

How to use the Angular RouterToday 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 routesRouter outletSpecial routesData and ActivatedRouteResolve guardCanXXX guards1. Define routesTo 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 modulesFor example we can define two routes which we add to the main module:const routes: Routes = [ { path: 'home', component: MainPageComponent, }, { path: '', redirectTo: &#…

Reactive form with Angular

Reactive form with AngularI 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. Create a rea…

How to declare a component in angular

How to declare a component in angularLast 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. ParametersComponent 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 componentHere are some of the parameter used to defined a component:styles for inline style in the componenttemplate to define inline templateFor example here is a simple component:@Component({ selector: 'app-square', template: '<div></div>', styles: [ &…

How does NgModule work in Angular

How does NgModule work in AngularThe first time I saw the NgModule decorator and its arguments I was completely lost.
I couldn’t understand what was the meaning of imports, declarations, exports, providers or bootstrap and I had a hard time finding clear explanations.
So today I will go through each attributes and provide an explanation together with an example to understand what is the role of each NgModule argument.1. How does a NgModule declaration look like? 2. Imports 3. Exports 4. Declarations 5. Providers The full source code is available on my GitHub How does a NgModule declaration look like?Angular utilises the typescript decorators to define NgModule.
It accepts imports, declarations, exports and providers.A typical declaration would look like that:import { CommonModule } from '@angular/common'; import { NgModule } from '@angular/core'; import { SquareComponent } from './square.component'; import { SquareServ…

Saving data with Entity Framework Core with SQLite

Saving data with Entity Framework Core with SQLiteEntity Framework is a framework abstracting away all the complexity of dealing with storage. This abstraction is also known as ORM ~ object-relational mapping.
There is a number of provider which are implementation of the storage like SQL server or MySql or also SQLite, the one we will be seeing in this post.
SQLite is a embedded database. The whole database is contained within a single .db file which makes it highly portable, so portable that it is the default database installed in mobile OS like iOS and Android. It is extremely easy to use and to maintain. It also offer a powerful implementation of SQL. Today we will see how we can make use of Entity Framework with SQLite provider in a ASP.NET Core application. 1. Install EF and create a new DbContext 2. Create migrations 3. Use in ASP NET Core 1. Install EF and create a new DbContextStart by installing the packages:Install-Package Microsoft.EntityFrameworkCore.SQLite Once we have …

Razor syntax and helpers

Razor syntax and helpersRazor is a HTML templating engine which allows us to construct HTML pages from a combination of data and HTML markup. The power of Razor resides in its typesafety and support for well known operators, conditional ifelse and iterators forwhileforeach. It allows us to directly use our C# models in the templates and call functions accessible by the view. Together with intellisense, it is quick way to build UI.
Today we will explore some of the features from Razor: 1. Syntax 2. Layout and partial views 3. View components 4. Tag helpers 1. SyntaxRazor expressions can written in two ways, implicit or explicit notation.For implicit notation, we start with @ and then we directly have access to values either from the view itself or some we created in the view or static functions.@{ var a = 10; } <label>@a</label> One useful property of the view is the Model.
It gives access to the model linked to the page. By going @Model.XXX, we can access the prop…

Attribute route in ASP NET Core

Attribute route in ASP NET CoreAttribute route in ASP NET Core is an easy way to define URL routes for Web API projects. There can be instance where it gets confusing because of all the options provided. Today we will see the meaning of the different options and how they affect the constructed route. This post will be composed by 3 parts:1. Route attribute 2. Route values and token replacement 3. "/" or "" 1. Route attributeThere are two ways to define routing, convention and attribute. When both are set, attribute route takes priority over convention route. In this post we will only discuss attribute route.Attribute route is specified using the [Route(...)] attribute. The route attribute can be set on controllers and actions. Setting the attribute on a controller will prefix all action route by the controller route.So for example specifying the following:Route("values") public class ValuesController: Controler { Route("list") public IA…