Wednesday, January 25, 2017

Strongly typed Angular 2 components



Typescript, is a typed superset of the JavaScript language that is compiled to produce clean, simple cross-browser compatible JavaScript code, which can help when building and maintaining large-scale applications. Typescript has features of ES6 such as interfaces, classes and modules.


Note: To specify custom typing we can use an interface
In this example we have specified custom type for address using an interface.

Conditional statements in angular 2



  • { * } marks the directive as the structural directive
  • BrowserModule exposes ng-if and ng-for directives

Simple Angular 2 toggle function



Angular's *ngIf directive is not subject to style specificity constraints.  It's always safe to use regardless of your stylesheet.   However, it's worth noting that it's not functionally equivalent.  Rather than toggling the display property, it works by adding and removing template elements from the DOM.

Handling unique component style in angular 2 (CSS)


We have 2 options styles and stylesUrls . Both styles and stylesUrls can be used within

Simple angular 2 template


 In this example we have a component named as UserComponent.
 In the selector we need to specify the name of the view specified in index.html

Aanatomy of angular 2

Structure of Angular 2 application 


Relative paths with module Id in angular 2


We can specify absolute path directly in angular 2 as shown below


import {Component, OnInit} from '@angular/core';
//Import interface
import {IProduct} from './product';
//view for the component
@Component({
    selector:'pm-products',
    templateUrl:'app/products/product-list.component.html',
    styleUrls: ['app/products/product-list.component.css']

})

There are several downsides of using absolute path.
  • Harder to adjust folder structure.
  • Reuse the components
  • Some tools within angular require relative path