@directive
*
sign; for example,
*ngIf
, *ngSwitch
, and *ngFor
ngStyle
and ngClass
ng generate directive directive-name
or ng g directive directive-name
@NgModule
decorator,
imported from the @angular/core
package
AppModule
(by convention)
ng generate module module-name
or ng g module module-name
*ngIf="condition-or-boolean-expression"
<div *ngIf="orderForm.value.name=='demo'">display when name is 'demo'</div> <div *ngIf="a > b">display if a is more than b</div> <div *ngIf="str == 'yes'">displayed if str is the string 'yes'</div> <div *ngIf="myfunc()">displayed if myfunc() returns true</div>
ngIf
several times *ngSwitchCase="case-value"
<div class="container"> <div *ngIf="myVar == 'A'">Var is A</div> <div *ngIf="myVar == 'B'">Var is B</div> <div *ngIf="myVar != 'A' && myVar != 'B'">Var is something else</div> </div>can be transformed into
<div class="container" [ngSwitch]="myVar"> <div *ngSwitchCase="'A'">Var is A</div> <div *ngSwitchCase="'B'">Var is B</div> <div *ngSwitchDefault>Var is something else</div> </div>
[ngSwitch]
captures the value of myVar
variablengSwitchCase
directive to describe the known cases (or results)ngSwitchDefault
to handle all the other unknown cases.
Note: ngSwitchDefault
is optional.
*ngFor="let item of item-collection"
let item
specifies a (template) variable that is receiving
each element of the item-collection
array.
The item-collection
is the collection of items from the controller.this.students = [ { name: 'Mickey', email: 'mickey@uva.edu', major: 'CS', year: 'second' }, { name: 'Minney', email: 'minney@uva.edu', major: 'CS', year: 'third' }, { name: 'duh', email: 'duh@uva.edu', major: 'SWE', year: 'third' }, { name: 'huh', email: 'huh@uva.edu', major: 'SWE', year: 'second' } ];Each row of data can be rendered as a table in app.component.html
<h4>List of students</h4> <table> <thead> <tr> <th>Name</th> <th>Email</th> <th>Major</th> <th>Year</th> </tr> </thead> <tr *ngFor="let s of students"> <td>{{ s.name }}</td> <td>{{ s.email }}</td> <td>{{ s.major }}</td> <td>{{ s.year }}</td> </table>
this.studentsByMajor = [ { major: 'CS', students: [ { name: 'Mickey', email: 'mickey@uva.edu', year: 'second' }, { name: 'Minney', email: 'minney@uva.edu', year: 'second' }, ] }, { major: 'SWE', students: [ { name: 'duh', email: 'duh@uva.edu', year: 'third' }, { name: 'huh', email: 'huh@uva.edu', year: 'second' } ] } ];A nested directive may be used to iterate through the students for a given major.
<h4>List of students by major<h4> <div *ngFor="let m of studentsByMajor"> <h2>{{ m.major }}</h2> <table> <thead> <tr> <th>Name</th> <th>Email</th> <th>Year</th> </tr> </thead> <tr *ngFor="let s of students"> <td>{{ s.name }}</td> <td>{{ s.email }}</td> <td>{{ s.year }}</td> </tr> </table> </div>
[style.cssproperty]="value"
<div [style.background-color]="'blue'" [style.color]="'white'"> Use fixed white text on blue background </div>
[ngStyle]="{'cssproperty': 'value', 'anotherproperty': 'value'}"
<div [ngStyle]="{'background-color': 'blue', 'color': 'white'}"> Use fixed white text on blue background </div>
<div> <input type="text" name="color" value="{{color}}" #colorinput> </div> <div> <input type="text" name="fontSize" value="{{fontSize}}" #fontinput> </div> <button (click)="apply(colorinput.value, fontinput.value)"> Apply settings </button> <div> <span [ngStyle]="{'color': color, 'font-size.px': fontSize}"> {{color}} text </span> </div>
apply
method (that sets the new color and font size values) in
class declaration in
app.component.ts
color = 'black'; /* set default */ fontSize = 12; /* set default */ apply(color: string, fontSize: number): void { this.color = color; this.fontSize = fontSize; }
[ngClass]="{cssproperty: booelean-value}"
.bordered { border: 1px dashed black; background-color: #eee; }and a view in app.component.html
<div [ngClass]="{bordered: false}">This is never bordered</div> <div [ngClass]="{bordered: true}">This is always bordered</div>