Sunday, September 9, 2018

Angular - Components

Everything in angular is a component!

  • Components are the basic building blocks in Angular. 
  • A component in Angular allows us to create a reusable UI widget.
  • A component can be used inside another component - Nested Components
A component in Angular is a class with a view template and a decorator. In simple terms we can say a component in angular is composed of
o   Template
§  Defines the user interface.
§  Contains html, directive and data bindings)
o   Class
§  Contains the code for the view template.
§  Like any object oriented programming language like C# or Java, a class in Angular can contain properties and methods.
§  Properties contain data for the view template.
§  Methods contain logic for the view template

o   Decorator
§  Adds meta data to the class, for making it an Angular Component
§  Applying decorator to a class is similar to applying attributes to a class in C#.

Example: An Angular application always need to have a root component called AppComponent
In below example:



  • A class AppComponent is defined with export keyword to make that usable in other part of the application.  
  • This class has only one property called name and a value ”Angular 2” is stored in it.
  • To add the component decorator (provided by Angular) we need to use import keyword (which is similar to using keyword in C#) in below example we are importing “Component” from “angular/core”.
  • Now Apply “Component” Decorator to the class (right now we are just using two properties from the Component Decorator)
o   selector – contains the directive name, where we want this component to be displayed. In our example its inside index.html
o   template  - defines HTML required for the view. And HTML required should be inside Backtick character
  • {{ }} – databinding expression
  • At runtime the default content from <my-app> directive will be replaced with above defined component’s template.
PS: You can use single quotes or double quotes in place of Backtick character when view template HTML is in single line, if the view template HTML is in more than one line you must use backtick character else you will get an error.

We can have either an inline view template as above or an external view template as below (for the same above example):





The external template view, is referred with keyword 'templateUrl' in decorator. The path value is relative to the index.html (the file which uses the component)

Disadvantages of having inline view templates:
  • We loose VisualStudio editor intellisense, code-completion and formatting features.
  • TypeScript code is not easier to read and understand when it is mixed with the inline template HTML.
Advantages of having external view template:
  • We have  loose VisualStudio editor intellisense, code-completion and formatting features available.
  • Not only the code in TypeScript file (in our example app.component.ts) is clean but also easier to read and understand.
Angular 2 recommends to extract templates to a separate file (external view template), if  view template HTML is longer than 3 lines.

Saturday, September 8, 2018

Angular - Setting up Angular in Visual Studio 2017

In this post, I will be providing you the steps to set up Angular in Visual Studio 2017 

Step 1 : Install Node.js and npm

  • node version 4.6.x or greater
  • npm version 3.x.x or greater
  • Site to download: https://nodejs.org/en/download/
  • To check the versions installed run following commands from cmd prompt (if node and npm are not installed on the system, the commands will not run and it will say “unrecognized internal or external command)
    • node –v
    • npm –v
Step 2: You also need VS 2017 (or VS 2015 update 3) 

Step 3: Configure environment settings for node and nmp
  • Tools -> Options -> Projects and Solutions -> External Web Tools
  • Move global path entry above the dev environment entry. This tells VS that look for external tools like npm is global path before internal paths
Before:



After:



              
Step 4: Install TypeScript for VS 2017
  • Angular 2 requires TypeScript version 2.2.0 or later
  • Download TypeScript For visual Studio 2017 from here 
https://www.microsoft.com/en-ca/download/details.aspx?id=55258
For VS 2015 from here: 
https://www.microsoft.com/en-us/download/details.aspx?id=48593
  • To check the version of typescript installed in VS, go to help-> About 


Step 5: Download “Quick Start Files” from Angular web site 
https://github.com/angular/quickstart Click on “clone or download” button and download zip file

Step 6: Create an empty web application in Visual Studio

Step 7: Copy “Quick Start Files” in created web application:
  • We don’t need all the files  from extracted “Quick Start Files” only following are required to be copied to empty web application)
§  “scr” folder and all its content
§  bs-config.json
§  package.json
§  tslint.json

  • Copy all these 4 items to the root project folder of the web application solution
Step 8: Restore required packages
  • Right click on the package.json -> restore packages (if the restore option is not available , try restarting Visual Studio and do clean solution)
  • Caution: it restores all the packages in the projects and a new folder node_modules folder is also seen under the projects, make sure that you don’t include that in the project.

Step 9 : Run the Project from command prompt
  • Open Command prompt
  • Go to root project location
  • And then type nmp start
  • It does following things
§  Launch TypeScript compiler and compiles the application code
§  After that launches lite server which in turns launches the browser
  • Now go and do a changes in startup project file "app.component.ts" and save it. This will directly be reflected in the browser as the lite-server is listening to the changes we are doing in the project files.
  • At this point we don’t have an option to run the Project in Visual Studio by pressing F5 or Ctlr + F5 that’s due to the wrong required file (css, and js) locatiosn in index.html page to fix that do following:

§  Changes  <base href="/"> to  <base href="/src/">  
§  Change script location to have a forward “/”  before node_modules
<script src="/node_modules/core-js/client/shim.min.js"></script>           <script src="/node_modules/zone.js/dist/zone.js"></script>
<script src="/node_modules/systemjs/dist/system.src.js"></script>

§  We also need to make a change in systemjs.config.js - 'npm:': '/node_modules/




PS: Sometimes one might see this error in while building the project in VS:
"(TS) Property 'lift' in type 'Subject<T>' is not assignable to the same property in base type 'Observable<T>'.  Type '<R>(operator: Operator<T, R>) => Observable<T>' is not assignable to type '<R>(operator: Operator<T, R>) => Observable<R>'.    Type 'Observable<T>' is not assignable to type 'Observable<R>'.      Type 'T' is not assignable to type 'R'."

To solve this issue, inside file tsconfig.json, put it in "compilerOptions" and set it to true.


Angular - Introduction

This is the introductory post for the series of post I am gonna write on Angular - a front-end web application framework.

Before heading to Angular let me give a small note on its predecessor AngularJS: 
  • A JavaScript based open source front-end web application framework (maintained by google) – a library written in JavaScript.
  • Normally referred as Angulrar 1 or Angular1.X ( released on October 2010)
  • It’s perfect for SPAs (Single Page Applications) – a web application or a web site that interacts with the user by dynamically rewriting the current page rather than reloading entire new pages from server.
Even though Angular is successor of AngularJS but its completely different than AngularJS, since its rewritten from ground up using TypeScript, so you cannot directly upgrade your application to Angular from AngularJS.

Angular is commonly referred as Angular 2+ or Angular v2 and above.

Versions:
o   Angular 2 –  released on year 2016
o   Angular 4 –  released on early 2017
o   Angular 5 –  released on late 2017               
o   Angular 6 – released on year 2018

o   Angular 7 – future 

Difference between Angular and AngularJS
  •  Performance
    • Improved test-ability, dependency injection  
  • Support for Mobile Applications
    • AngularJS was not directly compatible on mobile devices, one had to use third party frameworks to make AngularJS application run on mobile devices on the other hand Angular 2 is written from ground up with the support for mobile devices, so any single page application built in Angular works across mobile and desktop devices.
  •  Component Based Development
    • Component based development is the future of web development and in Angular 2 everything is component.
    • Component facilitate code reuse.
    • Use of component makes Angular 2 more testable.
  • More language choices – with Angular 2 we have more language choices available to write the code, below is the list of supported languages
    • ECMAScript 5 (Javsactipt?)
    • ECMAScript6 (aka ES 2015)
    • TypeScript (the most popular language among the list, Angular itself is written in TypeScript. TypeScript has great support for ECMAScript 6 standard.)
    • Dart
    • PureScript
    • Elm etc