The ANGULAR2 view is dynamic, and without modifying the source data bound to the UI, we can change whether the data is rendered, rendered, or rendered in the form of instructions.
(1). The most commonly used directives provided by ANGULAR2 are:
1.*ngif= ' expression ', which is loaded or written in the current element by the result of expression operation.
2.*ngfor= ' expression and declarations ' loop input array that loads the render template as a view based on the data of each member
<li class= "Person-info-panel" *ngfor= "let person of availablepeople;let i = index" > <a devide-age-group [ Birthday]= "Person.birthday" (click) = "Updateseletedperson (person)" >
{{Person.name}},{{person.email}},{{person.phone | tailnumberlength:5}},{{person.birthday}}
</a> </li>
3.[ng-switch]= ' expression ' loads different templates according to the different case of expression results
<div [ng-switch]= "conditionexpression" > <template [ng-switch-when]= "Case1" >case1</template > <template [ng-switch-when]= "Case1" >case2</template> <template [Ng-switch-default] >default</template> </div>
(2) Custom directives, as follows we come from defining an instruction that divides the age segment according to the different birthdays of the input and renders the user information in different styles Devide-age-group
//age-group.serviceImport {injectable} from ' @angular/core '; @Injectable () Export class agegroupsvc{ Constructor () {}//The service returns different age group categories Getagebybirth (birthday:date) {let todayyear = (new Date ()) According to the input birthday. getUTCFullYear (); Let birthyear = Birthday.getutcfullyear (); Let-age = todayyear-birthyear; return age; } Getagegroupbybirth (birthday:date) {let-age = This.getagebybirth (BirthDay); if (age <) { return 1; } else if (age >= && Age <) { return 2; } else if (age >= && Age <) { return 3; } else{ return 4;}} }
Import {directive,simplechanges, Elementref, Renderer,input,onchanges,oninit,docheck} from ' @angular/core '; import { AGEGROUPSVC} from './age ' import set = Reflect.set; @Directive ({/* Available selectors include ' a[directivename] '; Execute the instruction only on an element of a tag that has a directivename attribute; ' Directivename '; execute the directive on an element of <directiveName></directiveName>; ' [ Diractivename] '; executes the directive on all elements with directivename attributes; */selector: ' [Devide-age-group] ',//define CSS selector for properties Select providers:[// Agegroupsvc as a service to be declared as a provider AGEGROUPSVC]}) Export class Agegroupdevidediretive implements oninit{Ngoninit (): void { } element:elementref; _agegroupsvc:agegroupsvc;//received the input birthday, through the Age-group service to get the age group category, according to different categories for displaying user information elements set different styles @Input (' BirthDay ') set Setbirthday (birthday:date) {Let Targetcolor = ' white '; Let _birthday = new Date (BirthDay); if (_birthday instanceof Date) {Let Agegroup = This._agegroupsvc.getagegroupbybirth (_birthday); Switch (agegroup) {Case 1:targetcolor = ' red '; Break Case 2:this.element. NativeElement.style.color = ' black '; Targetcolor = ' yellow '; Break Case 3:targetcolor = ' green '; Break Case 4:targetcolor = ' blue '; Break Default:break; }} this.element.nativeElement.style.backgroundColor = Targetcolor; } constructor (Element:elementref, Renderer:renderer, agegroupsvc:agegroupsvc) {this.element = element; This._agegroupsvc = agegroupsvc; ' Nativeelement ' is the direct reference-to-the-DOM element element.nativeElement.style.color = ' white '; }} @NgModule ({bootstrap: [AppComponent], declarations: [//Introducing Agegroupdevidediretive Agegroupdevidediretiv in the application module E]})
Devide-age-group is introduced as an attribute, angular the last step in the rendering of the current element is to execute the devide-age-group directive on that element <a Devide-age-group [birthday]=] Person.birthday "(click) =" Updateseletedperson (person) ">{{person.name}},{{person.email}},{{person.phone | Tailnumberlength:5}},{{person.birthday}}</a>
Angular2-starter-directives