Skip to content

Instantly share code, notes, and snippets.

@ashburnere
Last active June 6, 2019 20:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ashburnere/bf581177504e4b8d7f8eb6d3e066453e to your computer and use it in GitHub Desktop.
Save ashburnere/bf581177504e4b8d7f8eb6d3e066453e to your computer and use it in GitHub Desktop.
Angular tutorial

Template syntax

Angular's template syntax extends HTML and JavaScript.

Structural directives

Structural directives shape or reshape the DOM's structure, typically by adding, removing, and manipulating the elements to which they are attached. Any directive with an * is a structural directive.

*ngFor A structural directive that renders a template for each item in a collection.

<div *ngFor="let product of products">
</div>

*ngIf Renders the element only if the value exists. '[ngSwitch]' Switch statement

<div [ngSwitch]="hero?.emotion">
  <app-happy-hero    *ngSwitchCase="'happy'"    [hero]="hero"></app-happy-hero>
  <app-sad-hero      *ngSwitchCase="'sad'"      [hero]="hero"></app-sad-hero>
  <app-confused-hero *ngSwitchCase="'confused'" [hero]="hero"></app-confused-hero>
  <app-unknown-hero  *ngSwitchDefault           [hero]="hero"></app-unknown-hero>
</div>

Template expressions

{{...}} Interpolation refers to embedding expressions into marked up text.

<h3>
{{ product.name }}
</h3>

[ ] Property binding - binds a property to a HTML property

<a [title]="product.name + ' details'">
     {{ product.name }}
</a>

( ) Event binding is used to bind an event to a HTML event

 <button (click)="share()">
   Share
 </button>

Alternative event binding:

<button on-click="share()">
  Share
</button>

Links

https://stackblitz.com/@ashburnere

https://stackblitz.com/edit/angular-l6adbj

https://angular.io/start

https://angular.io/guide/template-syntax

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment