Îı¾²åÖµ
×é¼þÖеıäÁ¿:{{data}} ²åÖµ±í´ïʽ:{{1+1}} {{1+getVal()}} еÄÄ£°å±í´ïʽÔËËã·û,ÀýÈç |,?. ºÍ !
<img src="{{itemImageUrl}}">
<p>{{title}}</p>
<ul>
<li *ngFor="let itemof list">{{item.name}}</li>
</ul>
//½á¹û:Template <script>alert("evil never sleeps")</script> Syntax ä¯ÀÀÆ÷²»»á´¦ÀíHTML,¶øÊÇÔÑùÏÔʾËü
<p>{{evilTitle}}</p>
public title:string = 'my-project';
public list:any[] = [{name:'jie'},{name:'lei}]
public evilTitle:string = 'Template <script>alert("evil never sleeps")</script> Syntax';
²»ÄÜʹÓÃÄÇЩ¾ßÓлò¿ÉÄÜÒý·¢¸±×÷ÓÃµÄ JavaScript ±í´ïʽ,°üÀ¨:
- ¸³Öµ (=, +=, -=, ¡)
- ÔËËã·û,±ÈÈç new¡¢typeof »ò instanceof µÈ¡£
- ʹÓà ; »ò , ´®ÁªÆðÀ´µÄ±í´ïʽ
- ×ÔÔöºÍ×Ô¼õÔËËã·û:++ ºÍ ¨C
- һЩ ES2015+ °æ±¾µÄÔËËã·û
- ²»Ö§³ÖλÔËËã,±ÈÈç | ºÍ &
ÊôÐÔ°ó¶¨
Propety°ó¶¨
Òª°ó¶¨µÄÊôÐÔ½«ÆäÀ¨ÔÚ·½À¨ºÅ[]ÄÚ,·½À¨ºÅ [] ʹ Angular ½«µÈºÅµÄÓҲ࿴×÷¶¯Ì¬±í´ïʽ½øÐÐÇóÖµ¡£Èç¹û²»Ê¹Ó÷½À¨ºÅ,µÈºÅÓÒ²àÊÓΪ×Ö·û´®×ÖÃæÁ¿
- ¿ÉÓÃÓڰ󶨱êÇ©×Ô´øµÄÊôÐÔ
- °ó¶¨ngclass,¸Ä±äclassÖµ
- °ó¶¨innerHtml,ÊôÐ԰󶨻á¶Ô
<script> ±ê¼Ç½øÐкöÂÔ´¦Àí,²»ÏÔʾscript±ê¼Ç¼°°ü¹üµÄÄÚÈÝ - ÔÚ¸¸×Ó×é¼þ´«Öµ°ó¶¨Öµ
<img [src]="itemImageUrl2">
<tr><td [colSpan]="1 + 1">Three-Four</td></tr>
//±¨´í,²»ÄÜ°ó¶¨td×ÔÉí²»´æÔÚµÄÊôÐÔ
<tr><td [aa]="1 + 1">Three-Four</td></tr>
<button [disabled]="isUnchanged">Disabled Button</button>
<p [ngClass]="classes">blue</p> //class="aa bb"
<p [ngClass]="classstr">red</p>//class='red'
//½á¹ûºöÂÔscript±êÇ©¼°ÄÚÈÝ:Template Syntax
<p [innerHtml]='evilTitle'></p>
public classes:string[] = ['aa','bb']
public classstr:string = 'red'
public evilTitle:string = 'Template <script>alert("evil never sleeps")</script> Syntax';
Attribute°ó¶¨
ÔÚAttributeÃû³ÆÇ°¼ÓÉÏǰ׺attr.
//½á¹û:aa='2'
<p [attr.aa]='1+1'>123</p>
//½á¹û:aria-label='1234'
<button [attr.aria-label]="actionName"></button>
public actionName:string = '1234'
Àà°ó¶¨
°ó¶¨ÀàÐÍ | Óï·¨ | ÊäÈëÀàÐÍ | ·¶ÀýÊäÈëÖµ |
---|
µ¥Ò»Àà°ó¶¨ | [class.sale]=¡°onSale¡± | boolean | undefined | null | true, false | ¶àÖØÀà°ó¶¨ | [class]=¡°classExpression¡± | string
Record<string, boolean | undefined | null
Array | ¡°my-class-1 my-class-2¡±
{foo: true, bar: false}
[¡®foo¡¯, ¡®bar¡¯] |
°ó¶¨µ½µ¥¸öclass:
Òª´´½¨µ¥¸öÀà°ó¶¨,ÇëʹÓÃǰ׺ class ºó¸úÒ»¸öµãºÍ CSS ÀàµÄÃû³Æ¡£ÀýÈç [class.sale]=¡°onSale¡±¡£onSale ΪÕæֵʱÌí¼ÓÀà,ÔÚ±í´ïʽΪ¼Ùֵʱ(undefined ³ýÍâ)ɾ³ýÀà¡£
//½á¹û:class="scale¡° isFlagΪtrue,Ìí¼ÓÀàscale; Èç¹ûisFlagΪfalse,classûÓÐscale
<p [class.sale]="isFlag">sale111</p>
public isFlag:boolean = true
°ó¶¨¶à¸öclass
Òª°ó¶¨µ½¶à¸öÀà,ÇëʹÓà [class] À´ÉèÖñí´ïʽ - ÀýÈç,[class]=¡°classExpression¡±
//Óÿոñ·Ö¸ôµÄÀàÃû×Ö·û´®:class="class1 class2"
<p [class]='classSpace'>aaacccc</p>
//ÒÔÀàÃû×÷Ϊ¼üÃû½«Õæ»ò¼Ù±í´ïʽ×÷ΪֵµÄ¶ÔÏó: class="foo"
<p [class]='classObj'>111</p>
//ÀàÃûµÄÊý×é class="bar foo"
<p [class]='classArr'>222</p>
public classSpace:string = 'class1 class2'
public classObj = {foo: true, bar: false}
public classArr:string[]=['foo','bar']
°ó¶¨µ½style Attribute
°ó¶¨ÀàÐÍ | Óï·¨ | ÊäÈëÀàÐÍ | ·¶ÀýÊäÈëÖµ |
---|
µ¥Ò»Ñùʽ°ó¶¨ | [style.width]=¡°width¡± | string| undefined | null | ¡°100px¡± | ´øµ¥Î»µÄµ¥Ò»Ñùʽ°ó¶¨ | [style.width.px]=¡°width¡± | number| undefined | null | 100 | ¶àÖØÑùʽ°ó¶¨ | [style]=¡°styleExpression¡± | string
Record<string, string| undefined | null | ¡°width: 100px; height: 100px¡±
{width: ¡®100px¡¯, height: ¡®100px¡¯} |
µ¥Ò»Ñùʽ°ó¶¨
¿ÉÒÔÓÃÖÐÏ߸ñʽ»ò ÍÕ·å¸ñʽ±àдÑùʽ Attribute Ãû¡£
//½á¹û:style="width: 100px;"
<p [style.width]="'100px'">ww</p>
//½á¹û:style="width: 300px;"
<p [style.width]="width">ww1</p>
//ʹÓÃÍÕ·å¸ñʽ,½á¹û:style="margin-top: 40px;"
<p [style.marginTop]="showHeader ? '40px' : '0px'">1111</p>
//ʹÓÃÖÐÏ߸ñʽ,½á¹û:style="background-color: red;"
<nav [style.background-color]="bgColor">222</nav>
public width:string = '300px'
public showHeader:boolean = true
public bgColor:string = 'red'
´øµ¥Î»µÄµ¥Ò»Ñùʽ°ó¶¨
//½á¹û:style="height: 100px; max-height: 50px;"
<p [style.height.px]="100" [style.max-height.px]="50">122223</p>
¶àÖØÑùʽ°ó¶¨
ÒªÇ뻶à¸öÑùʽ,Çë°ó¶¨µ½ [style] Attribute,ÀýÈç [style]=¡°styleExpression¡± ¡£ styleExpression ¿ÉÒÔÊÇÒ»¸ö×Ö·û´®,Ò²¿ÉÒÔÊÇÒ»¸ö¶ÔÏó¼üÃûÊÇÑùʽÃû,ֵΪÑùʽֵ
//×Ö·û´®¸ñʽ,½á¹û:style="color: blue; font-size: 12px;"
<p [style]="navStyle">11</p>
//¶ÔÏó¸ñʽ,½á¹û:style="height: 100px; width: 100px;"
<p [style]="styleobj">www</p>
public navStyle:string = 'font-size: 12px; color: blue;';
public styleobj = {width: '100px', height: '100px'}
[class.foo] )½«ÓÅÏÈÓÚ²»Ìض¨ [class] µÄ°ó¶¨, [style.bar] )½«ÓÅÏÈÓÚ²»Ìض¨ [style] µÄ°ó¶¨¡£ °ó¶¨»áÓÅÏÈÓÚ¾²Ì¬ÊôÐÔ:class ºÍ [class] ¾ßÓÐÏàËƵÄÌØÒìÐÔ,µ«ÊÇ [class] °ó¶¨¸üÓÅÏÈһЩ,ÒòΪËüÊǶ¯Ì¬µÄ
Ä£°åÒýÓñäÁ¿
Ä£°åÒýÓñäÁ¿ #customerInput
<input #customerInput>{{customerInput.value}}
ʼþ°ó¶¨
Ä£°åÓï¾ädeleteHero(),³öÏÖÔÚ = ºÅÓÒ²àµÄÒýºÅÖС£ Ä£°åÓï¾ä½âÎöÆ÷ÌرðÖ§³Ö»ù±¾¸³Öµ = ºÍ´øÓзֺŠ; µÄ´®Áª±í´ïʽ
<button (click)="deleteHero()">Delete hero</button>
//½ÓÊÕÄ£°å×Ô¼ºµÄ$event¶ÔÏóÓÃ×÷²ÎÊý
<button (click)="onSave($event)">Save</button>
//½ÓÊÕÄ£°åÊäÈë±äÁ¿item
<button *ngFor="let item of list" (click)="deleteHero(item)">{{item.name}}</button>
//½ÓÊÕÄ£°åÒýÓñäÁ¿#heroForm
<form #heroForm (ngSubmit)="onSubmit(heroForm)"> ... </form>
¹ÜµÀ
¹ÜµÀÓÃÀ´¶Ô×Ö·û´®¡¢»õ±Ò½ð¶î¡¢ÈÕÆÚºÍÆäËûÏÔʾÊý¾Ý½øÐÐת»»ºÍ¸ñʽ»¯¡£ ¹ÜµÀÊÇһЩ¼òµ¥µÄº¯Êý,¿ÉÒÔÔÚÄ£°å±í´ïʽÖÐÓÃÀ´½ÓÊÜÊäÈëÖµ²¢·µ»ØÒ»¸öת»»ºóµÄÖµ
Angular ΪµäÐ͵ÄÊý¾Ýת»»ÌṩÁËÄÚÖõĹܵÀ: https://angular.cn/api
ÄÚÖõĹܵÀ | ¹ÜµÀÃû×Ö | ×÷Óà | Á´½Ó |
---|
DatePipe | date | ¸ù¾Ý±¾µØ»·¾³ÖеĹæÔò¸ñʽ»¯ÈÕÆÚÖµ | DatePipe | UpperCasePipe | uppercase | °ÑÎı¾È«²¿×ª»»³É´óд | | LowerCasePipe | lowercase | °ÑÎı¾È«²¿×ª»»³ÉСд | | CurrencyPipe | currency | °ÑÊý×Öת»»³É»õ±Ò×Ö·û´®,¸ù¾Ý±¾µØ»·¾³ÖеĹæÔò½øÐиñʽ»¯ | | DecimalPipe | decimal | °ÑÊý×Öת»»³É´øСÊýµãµÄ×Ö·û´®,¸ù¾Ý±¾µØ»·¾³ÖеĹæÔò½øÐиñʽ»¯ | | PercentPipe | percent | °ÑÊý×Öת»»³É°Ù·Ö±È×Ö·û´®,¸ù¾Ý±¾µØ»·¾³ÖеĹæÔò½øÐиñʽ»¯ | | SlicePipe (ÖÁÉÙÒ»¸ö²ÎÊý) | slice | ½ØÈ¡Ò»¸öÐÂÊý×é»ò×Ö·û´® | |
ÔÚÄ£°å±í´ïʽÖÐʹÓùܵÀ²Ù×÷·û(|),½ô½Ó×ÅÊǹܵÀµÄÃû×Ö¡£
- ¹ÜµÀ²ÎÊý: ( Òª´¦ÀíµÄÖµ | ¹ÜµÀÃû×Ö:²ÎÊýÖµ)ÀýÈç:{{ amount | currency:¡®EUR¡¯ }}
- ¹ÜµÀ¿É½ÓÊܶà¸ö²ÎÊý:ÓÃðºÅ·Ö¸ôÖµ,ÀýÈç:{{ amount | currency:¡®EUR¡¯:'Euros '}},»á°ÑµÚ¶þ¸ö²ÎÊýEuros Ìí¼Óµ½Êä³ö×Ö·û´®ÖÐ
- ¹ÜµÀ´®Áª:Ò»¸ö¹ÜµÀµÄÊä³ö»á³ÉΪÏÂÒ»¸ö¹ÜµÀµÄÊäÈë¡£ÀýÈç:{{ birthday | date | uppercase}}
//Apr 15, 1988
<p>{{ birthday | date}}</p>
//APR 15, 1988 ¹ÜµÀ´®Áª°Ñbirthday¸ñʽ»¯ÎªApr 15, 1988,ÔÙ°Ñ×Öĸ´óдת»¯ÎªAPR 15, 1988
<p>{{ birthday | date | uppercase}}</p>
//04/15/88 ´«²Î¸ñʽ»¯ÈÕÆÚ:ÔÂ/Ìì/Äê
<p>{{ birthday | date:"MM/dd/yy" }} </p>
//1988-04-15 00:00
<p>{{ birthday | date: 'yyyy-MM-dd HH:mm'}}</p>
// €10.00 °Ñamountת»»³ÉÅ·Ôª
<p>{{ amount | currency:'EUR' }}</p>
//Euros 10.00 »á°ÑµÚ¶þ¸ö²ÎÊýEuros Ìí¼Óµ½Êä³ö×Ö·û´®ÖÐ
<p>{{ amount | currency:'EUR':'Euros '}}</p>
//y-project ½ØÈ¡³öÒ»¸öÐÂ×Ö·û´®,´Ó1ϱ꿪ʼµ½½áβ
<p>{{title | slice:1}}</p>
//y-pr ´Óϱê1¿ªÊ¼µ½Ï±ê5½áβ,²»°üº¬½áβ5
<p>{{title | slice:1:5}}</p>
//2,a,5,true ½ØÈ¡³öÒ»¸öÐÂÊý×é,´Ó1ϱ꿪ʼµ½½áβ
<p>{{arr | slice:1}}</p>
public birthday = new Date(1988, 3, 15);
public amount:number = 10
public title:string = ''my-project
public arr:any[] = [1,2,'a',5,true]
}
×Ô¶¨Òå¹ÜµÀ
- ʹÓà @Pipe×°ÊÎÆ÷ °ÑÒ»¸öÀà±ê¼ÇΪһ¸ö¹ÜµÀ
- ÔÚ×Ô¶¨Òå¹ÜµÀÀàÖÐʵÏÖ PipeTransform ½Ó¿ÚÀ´Ö´ÐÐת»»¡£
Angular µ÷Óà transform ·½·¨,¸Ã·½·¨Ê¹Óð󶨵ÄÖµ×÷ΪµÚÒ»¸ö²ÎÊý,°ÑÆäËüÈκβÎÊý¶¼ÒÔÁбíµÄÐÎʽ×÷ΪµÚ¶þ¸ö²ÎÊý,²¢·µ»Øת»»ºóµÄÖµ¡£
//exponential-strengthPipe.pipe.ts
import { Pipe, PipeTransform } from '@angular/core';
/*Ö¸Êý¼¶×ª»»,±ÈÈç2µÄ10´Î·½×ª»»Îª1024
* ²ÎÊý:value
* exponentĬÈÏΪ1
*
* ¸ñʽ:
* {{ value | exponentialStrength:exponent}}
* formats to: 1024
*/
@Pipe({name: 'exponentialStrength'})
export class ExponentialStrengthPipe implements PipeTransform {
transform(value: number, exponent = 1): number {
return Math.pow(value, exponent);
}
}
//1024 Àý×Ó
<p> {{2 | exponentialStrength: 10}}</p>
|