Outils du site

De quelle couleur est un caméléon quand il se regarde dans la glace ? [Inconnu]

56-tools:angular

Angular

Angular client

Angular with Pug

Ressources

Tri table example

Data binding

[(ngModel)] Two-way data binding.
[property] Property binding.
(event) Event binding

Interpolation

{{variable}} interpolation. Les deux formes ci-dessous sont équivalentes:

  • <img src=“{{heroImageUrl}}”> is the interpolated image.
  • <img [src]=“heroImageUrl”> is the property bound image.

Property binding [property]

It flows a value in one direction, from a component's data property into a target element property.

You cannot use property binding to pull values out of the target element.

You can't bind to a property of the target element to read it. You can only set it.

Event binding (event)

Les deux formes ci-dessous sont équivalentes:

  • <button (click)=“onSave()”>Save</button>
  • <button on-click=“onSave()”>Save</button>

two-way data binding [(ngModel)]

You often want to both display a data property and update that property when the user makes changes.

Template reference variables ( #var )

variable locale qui peut être utilisée par la suite :

<input #phone placeholder="phone number">

<!-- phone refers to the input element; pass its `value` to an event handler -->
<button (click)="callPhone(phone.value)">Call</button>

Directives structurelles

*ngIf

Micro syntax:

<div *ngIf="hero" class="name">{{hero.name}}</div>

Equivallent full syntax:

<ng-template [ngIf]="hero">
  <div class="name">{{hero.name}}</div>
</ng-template>

*ngFor

Micro code:

<div *ngFor="let hero of heroes; let i=index; let odd=odd; trackBy: trackById" [class.odd]="odd">
  ({{i}}) {{hero.name}}
</div>

Equivalent full syntax:

<ng-template ngFor let-hero [ngForOf]="heroes" let-i="index" let-odd="odd" [ngForTrackBy]="trackById">
  <div [class.odd]="odd">({{i}}) {{hero.name}}</div>
</ng-template>

ngSwitch

Micro code:

<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="'app-confused'" [hero]="hero"></app-confused-hero>
  <app-unknown-hero  *ngSwitchDefault           [hero]="hero"></app-unknown-hero>
</div>

Equivalent full syntax:

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

Input and Output properties

An Input property is a settable property annotated with an @Input() decorator. Values flow into the property when it is data bound with a property binding [property].

Exemple:

  • Declaration: @Input() hero: Hero;
  • Usage: <app-hero-detail [hero]=“selectedHero”></app-hero-detail>
  • Autre forme d'écriture: bind-hero=“selectedHero”

An Output property is an observable property annotated with an @Output() decorator. The property almost always returns an Angular EventEmitter. Values flow out of the component as events bound with an event binding (event).

Components

Remember that all components are directives.

Directives

There are three kinds of directives in Angular:

  • Components directives: with a template.
  • Structural directives: change the DOM layout by adding and removing DOM elements.
  • Attribute directives: change the appearance or behavior of an element, component, or another directive.

Testing

Exemples et tests

Dernière modification : 2018/04/08 20:48