Creating Components import { Component } from '@angular/core' @Component({ selector: 'courses', template: '<h2>Courses</h2>' }) export class Component { } Then, register them in AppModule (or the module the belong to): @NgModule({ declarations: [CoursesComponent] }) export class AppModule { … } Using Components import { CoursesComponent } from './courses.component' @Component({ template: '<courses></courses>' }) Templates Interpolation syntax: {{ title }} Displaying lists: <ul> <li *ngFor=“let course of courses”> {{ course }} </li> </ul> Services import { Injectable } from '@angular/core'; @Injectable() export class CourseService { } Dependency Injection Register a service as a provider in AppModule (or the module it belongs to): @NgModule({ providers: [CourseService] }) export class AppModule { … } Then, inject it into the constructor of the components that need it: export class CourseComponent { constructor(courseService: CourseService) {} } Directives Basic structure import { Directive } from '@angular/core' @Directive({ selector: '[autoGrow]', host: { '(focus)': 'onFocus()', '(blur)': 'onBlur()' } }) export class AutoGrowDirective { onFocus() { … } onBlur() { … } } To access and modify DOM elements import { ElementRef, Renderer } from '@angular/core' export class AutoGrowDirective { constructor( private el: ElementRef, private renderer: Renderer) { } onFocus(){ this.renderer.setElementStyle(this.el.nativeElement, 'width', '200'); } } Registration Once you implement a directive, you should register it in AppModule (or the module it belong to): @NgModule({ declarations: [AutoGrowDirective] }) export class AppModule { } Angular Bindings Interpolation <h1>{{ title }}</h1> Property binding <img [src]="imageUrl" /> <img bind-src="imageUrl" /> Class binding <li [class.active]="isActive" /> Style binding <button [style.backgroundColor]="isActive ? 'blue' : 'gray'"> Event binding <button (click)="onClick($event)"> <button on-click="onClick($event)"> Two-way binding <input type="text" [(ngModel)]="firstName"> <input type="text" bindon-ngModel="firstName"> Input Properties Using @Input annotation import { Input } from ‘@angular/core’; @Component(…) export class FavoriteComponent { @Input(‘is-favorite’) isFavorite = false; } Using component metadata @Component({ inputs: [‘isFavorite:is-favorite’] }) export class FavoriteComponent { isFavorite = false; } In the host component <favorite [is-favorite]=“post.isFavorite”></favorite> Output Properties Using @Output annotation import { Output } from ‘@angular/core’; @Component(…) export class FavoriteComponent { @Output(‘favorite-change’) change = new EventEmitter(); onClick() { this.change.emit({ newValue: this.isFavorite }); } } Using component metadata @Component({ outputs: [‘change:favoriteChange’] }) export class FavoriteComponent { change = new EventEmitter(); onClick() { this.change.emit({ newValue: this.isFavorite }); } } In the host component <favorite (favoriteChange)=“onChange()”></favorite> Templates @Component({ template: ‘…’, // or templateUrl: ‘app/template.template.html’ }) Styles @Component({ styles: [‘…’], styleUrls: [‘…’, ‘…’]; })
2.12.2017
Angular Cheat Sheet
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment