Angular - goatandsheep/goatandsheep.github.com GitHub Wiki

Tips

  • put your non-AppComponent classes above @Component()

CLI

  • Installation: sudo npm install -g @angular/cli
  • ng new <app name>: sets up new angular application and directory structure
  • ng serve: simulates app locally
    • --open: opens the simulation in your default browser
  • ng build:
    • creates dist folder

Module

consolidates components, directives, services, and pipes into cohesive blocks of functionality, each focused on a feature area, application business domain, workflow, or common collection of utilities.

Declaration

the declarations array contains a list of application components, pipes, and directives that belong to the module. A component must be declared in a module before other components can reference it

HTML Magic

{{ variable }} prints a variable

*ngFor="let i of arr"

*ngIf="bool": conditionally renders component

Built-in functions

  • ngSubmit: no worries about blocking default form behaviour
  • ngStyle
  • ngClass
  • ngModel

Components

Putting square brackets around an attribute name, to the left of the equal sign (=), makes it the target of a property binding expression. You must declare a target binding property to be an input property. Otherwise, Angular rejects the binding and throws an error.

<fa-icon (click)="onClick" [ngStyle]="{'color': 'red'}" [icon]="faTimes"></fa-icon>

Events are handled with round brackets

npx ng generate component components/newComponent

Properties

A class property

@Input() property:type = "default"

Events

@Output() event = new EventEmitter()

Bubbles events to parent component

@Injectable

A class decorator that allows you to inject services

Pipe

transforms displayed values within a template

Syntax: {{ value | pipe }}

Similar to filters from AngularJS, but no orderBy nor filter

Services

must add it to your constructor

constructor(private uiService: UiService) {}

npx ng generate service services/newService

Router

routerLink

<router-outlet>

conditionally render features on some pages: if (this.router.link === '/')

Observables

subscribe

Forms

To do the 2-way data binding, do <input [(ngModel)]="variableName">

⚠️ **GitHub.com Fallback** ⚠️