ANGULAR 2 Ar c hi t ec t ur eov er v i ew Angul ari saf r amewor kf orbui l di ngc l i entappl i c at i onsi nHTMLandei t herJ av aSc r i pt oral anguagel i k eT ypeSc r i ptt hatc ompi l est oJ av aSc r i pt . Thef r amewor kc ons i s t sofs ev er al l i br ar i es ,s omeoft hem c or eands omeopt i onal . Youwr i t eAngul arappl i c at i onsbyc ompos i ngHTMLt empl at eswi t hAngul ar i z ed mar k up,wr i t i ngc omponentc l as s est omanaget hos et empl at es ,addi ngappl i c at i on l ogi ci ns er v i c es ,andbo xi ngc omponent sands er v i c esi nmodul es . Theny oul aunc ht heappb yboot s t r appi ngt her ootmodul e.Angul art ak esov er , pr es ent i ngy ourappl i c at i onc ont enti nabr ows erandr es pondi ngt ous eri nt er ac t i ons ac cor di ngt ot hei ns t r uc t i onsyou' v epr ov i ded. Ofc our s e,t her ei smor et oi tt hant hi s .You' l l l ear nt hedet ai l si nt hepagest hat f ol l ow.Fornow,f oc usont hebi gpi c t ur e.
Thear c hi t ec t ur edi agr am i dent i fi est heei ghtmai nbui l di ngbl oc ksofanAngul ar appl i c at i on:
•
Modul es
•
Comp mponent s
•
T emp l a t e s
•
Met adat a
•
Dat abi ndi ng
•
Di r ec t i v es
•
Ser v i c es
•
Dependenc yi nj ec t i on Lear nt hes ebui l di ngbl oc k s,andy ou' r eony ourway . Thec oder ef er enc edont hi spagei sav ai l abl easal al i v eex ampl e.
Modul es
Angul a rappsar emodul arandAngul arhasi t sownmodul ar i t ys y s t em c al l edAngul ar modul esorNgMo Modul es. Angul armodul esar eabi gdeal .Thi spagei nt r oduc esmo modul es ;t heAngul armodul es pagec o v er st hem i ndep t h. Ev e r yAngul ara pphasatl eas toneAngul armodul ec l as s ,t her ootmodul e, c on v ent i onal l yna me medAppModule. Whi l et her oo tmodul ema ybet heonl ymo modul ei nas ma mal l appl i c at i on,mos tapps ha v ema nymo mor ef ea t u r emod ul e s ,e ac hac o hes i v ebl o c ko fc o dede di c a t e dt oan appl i c at i ondomai n,awor k fl ow,orac l os el yr el at eds etofc apabi l i t i es . AnAngul armodul e,whet herar ootorf eat ur e,i sac l as swi t han@NgModule
•
Modul es
•
Comp mponent s
•
T emp l a t e s
•
Met adat a
•
Dat abi ndi ng
•
Di r ec t i v es
•
Ser v i c es
•
Dependenc yi nj ec t i on Lear nt hes ebui l di ngbl oc k s,andy ou' r eony ourway . Thec oder ef er enc edont hi spagei sav ai l abl easal al i v eex ampl e.
Modul es
Angul a rappsar emodul arandAngul arhasi t sownmodul ar i t ys y s t em c al l edAngul ar modul esorNgMo Modul es. Angul armodul esar eabi gdeal .Thi spagei nt r oduc esmo modul es ;t heAngul armodul es pagec o v er st hem i ndep t h. Ev e r yAngul ara pphasatl eas toneAngul armodul ec l as s ,t her ootmodul e, c on v ent i onal l yna me medAppModule. Whi l et her oo tmodul ema ybet heonl ymo modul ei nas ma mal l appl i c at i on,mos tapps ha v ema nymo mor ef ea t u r emod ul e s ,e ac hac o hes i v ebl o c ko fc o dede di c a t e dt oan appl i c at i ondomai n,awor k fl ow,orac l os el yr el at eds etofc apabi l i t i es . AnAngul armodul e,whet herar ootorf eat ur e,i sac l as swi t han@NgModule
d e c o r a t o r . Dec or at or sar ef unc t i onst hatmodi f yJ av aSc r i ptc l as s es .Angul arhasma many dec or at or st hatat t ac hmet adat at oc l as s ess ot hati tk nowswhatt hos ec l as s esme mean a ndho wt h eys h ou l dwor k .Lear nmo mor e aboutdec or at or sont heweb. i sadec or at orf unc t i ont hatt ak esas i ngl emet adat aobj ec twhos e NgModule pr oper t i esdes c r i bet hemodul e.Themos ti mpor t antpr oper t i esar e: •
declarations -t hev i ewc l as s est hatbel ongt ot hi smo modul e.Angul arhast hr eek i ndsof
v i ewc l as ses :c omp mponent s ,di r ec t i v es ,andpi pes . •
-t hes ubs etofdec l ar at i onst hats houl dbev i s i bl eandus abl ei nt he exports componentt empl at es ofot hermodul es .
•
-ot hermodul eswhoseexpor t edcl assesar eneededbycomp mponent imports t empl at esdec l ar edi nt hi smo modul e.
•
providers -c r eat or sofs er v i c es t hatt hi smodul ec ont r i but est ot hegl obal c ol l ec t i onof
s er v i c es ;t heybec omeac ces si bl ei nal l par t soft heapp. •
-t hemai nappl i c at i onv i ew,c al l edt her ootc omponent ,t hathos t sal lot her bootstrap appv i ews .Onl yt her ootmodul es houl ds ett hi sbootstrap pr oper t y . Her e' sas i mpl er ootmodul e: app/ app. modul e. t s import { import { NgModule NgModule } } from @angular!core @angular!core"" import { import { #ro$serModule #ro$serModule } } from @angular!platform%bro$ser @angular!platform%bro$ser"" @NgModule&{ @NgModule &{ imports' ( #ro$serModule #ro$serModule )* )* providers' ( Logger Logger )* )* declarations' ( App+omponent App+omponent )* )* exports' ( App+omponent App+omponent )* )* bootstrap' ( App+omponent App+omponent ) ) },
export class AppModule { }
Theexport ofApp+omponent i sj us tt os howhowt oex por t ;i ti s n' tac t ual l ynec es sar y i nt hi sex ampl e.Ar ootmodul ehasnor eas ont oe xpor tan yt hi ngbec aus eot her c ompo nent sdon' tne edt oi mpor tt her oo tmodul e. Launc hanappl i c at i onbyboot s t r appi ngi t sr ootmodul e.Dur i ngdev el opmenty ou' r e l i k el yt oboot s t r apt heAppModule i namain-ts fil el i k et hi sone. app/ mai n. t s import { platform#ro$ser./namic } from @angular!platform%bro$ser%d/namic" import { AppModule } from -!app-module"
platform#ro$ser./namic&,-bootstrapModule&AppModule,"
Angul armodul esv s .J a v aSc r i ptmodul es TheAngul armodul e— acl assdecor at edwi t h@NgModule —i saf undament al f eat ur eofAngul ar . J av aSc r i ptal s ohasi t sownmodul es y st em f ormanagi ngc ol l ec t i onsofJ av aSc r i pt obj ec t s .I t ' scompl et el ydi ffer entandunr el at edt ot heAngul armodul es ys t em. I nJ av aSc r i pteac hfi l ei samodul eandal l obj ec t sdefi nedi nt hefi l ebel ongt ot hat modul e.Themodul edec l ar ess omeobj ec t st obepubl i cb ymar k i ngt hem wi t ht he export k e ywor d.Ot herJ av aSc r i ptmodul esus ei mpor ts t at ement st oac c es spubl i c
obj ec t sf r om ot hermodul es . import { NgModule } from @angular!core" import { App+omponent } from -!app-component" export class AppModule { }
Lear nmor eaboutt heJavaScr i ptmodul esys t em ont heweb. Th es ear et wodi ff er e nta ndc omp l e me nt ar ymo du l es y s t e ms .Us et he m bo t ht owr i t e y ourapps .
Angul arl i br ar i es
Angul ars hi psasac ol l ec t i onofJ av aSc r i ptmodul es .Youc ant hi nkoft hem asl i br ar y mo du l e s . Eac hAngul arl i br ar ynamebegi nswi t ht he@angular pr efi x. Youi ns t al lt hem wi t ht henpm pac k agemanagerandi mpor tpar t soft he m wi t h J av aSc r i ptimport s t at ement s . Forex ampl e,i mpor tAngul ar ' s+omponent dec or at orf r om t he@angular!core l i br ar y l i k et hi s : import { +omponent } from @angular!core"
Youal s oi mpor tAngul armodul esf r om Angul arl i br ar i esus i ngJ av aSc r i pti mpor t s t at ement s : import { #ro$serModule } from @angular!platform%bro$ser"
I nt hee x ampl eoft hes i mpl er ootmodul eabo v e,t heappl i c at i onmodul eneeds mat er i al f r om wi t hi nt hat#ro$serModule.Toac c es st hatmat er i al ,addi tt ot he met adat aimports l i k et hi s . @NgModule imports'
( #ro$serModule )*
I nt hi swa yy ou' r eus i ngbot ht heAngul arandJ av aSc r i ptmodul es y st emst oget her . I t ' seas yt oc onf us et het wos y s t emsbec aus et he ys har et hec ommonv o c abul ar yof " i mpor t s "and" ex por t s " .Hangi nt her e.Thec onf us i ony i el dst oc l ar i t ywi t ht i meand
ex per i enc e. Lear nmor ef r om t heAngul armodul es p age.
Component s
Ac omponentc ont r ol sapat c hofs c r eenc al l edav i ew. Forex ampl e,t hef ol l owi ngv i ewsar ec ont r ol l edbyc omponent s : •
Theappr ootwi t ht henavi gat i onl i nks.
•
Thel i s tofher oes .
•
Theher oedi t or . Youdefi neac omponent ' sappl i c at i onl ogi c —whati tdoest os uppor tt hev i ew—i ns i de ac l as s.Thec l as si nt er ac t swi t ht hev i ewt hr oughanAPIofpr oper t i esandmet hods . Forex ampl e,t hi s0eroList+omponent hasa1eroes pr oper t yt hatr et ur nsanar r ayof her oest hati tac qui r esf r om as er v i c e.0eroList+omponent al s oha saselect0ero&, me t hodt hats e t saselected0ero pr oper t ywhent heus erc l i c k st oc hoos eaher of r om t hatl i s t . app/ her ol i s t . c omponent . t s( c l as s) export class 0eroList+omponent implements n3nit { 1eroes' 0ero()" selected0ero' 0ero"
constructor&private service' 0ero4ervice, { } ngn3nit&, {
t1is-1eroes 5 t1is-service-get0eroes&," } select0ero&1ero' 0ero, { t1is-selected0ero 5 1ero" } }
Angul a rc r eat es ,updat es ,anddes t r o y sc omponent sast h eus ermo v est hr ought he appl i c at i on.Yourappc ant ak eac t i onateac hmomenti nt hi sl i f ec y cl et hr ough opt i onal l i f ec y cl ehook s ,l i k engn3nit&, dec l ar edabo v e.
T empl at es
Youdefi neac omponent ' sv i e w wi t hi t sc ompani ont empl at e.At empl at ei saf or m of HTMLt ha tt el l sAngul a rho wt or endert hec omponent . At empl at el ook sl i k er egul arHTML,e x ceptf oraf ewdi ffer enc es .Her ei sat empl at e f orour0eroList+omponent: app/ her ol i s t . c omponent . ht ml • • • • • • • •
61270ero List6!127 6p76i78ic9 a 1ero from t1e list6!i76!p7 6ul7 6li :ng;or5
Al t hought hi st empl at eus est y pi c al HTMLel ement sl i k e6127 and6p7,i tal s ohas s omedi ff er enc es .Codel i k e:ng;or,{{1ero-name}} ,&clic9,,(1ero),and61ero% detail7 u sesAngul ar ' st empl at es y nt ax .
I nt hel as tl i neoft het empl at e,t he61ero%detail7 t agi sac us t om el ementt hat r epr esent sanewcomponent ,0ero.etail+omponent. The0ero.etail+omponent i sadi ff er entc omponentt hant he0eroList+omponent y ou' v ebeenr ev i ewi ng.The0ero.etail+omponent ( c odenots hown)pr es ent sf ac t s aboutapar t i c ul arher o,t heher ot hatt heus ers el ec t sf r om t hel i s tpr es ent edbyt he e0ero.etail+omponent i sac hi l doft he0eroList+omponent. 0eroList+omponent.Th
Not i cehow61ero%detail7 r est scomf or t abl yamongnat i veHTMLel ement s.Cust om c o mpo ne nt smi xs ea ml es s l ywi t hna t i v eHTMLi nt hes a mel a y o ut s .
Met adat a
Met adat at el l sAngul arhowt opr oc es sac l as s . Look i ngbac katt hec ode f or0eroList+omponent,y ouc ans eet hati t ' sj us tac l as s . Ther ei snoev i denc eofaf r amewor k ,no" Angul ar "i ni tatal l . I nf ac t ,0eroList+omponent r eal l yi sj us tac l as s .I t ' snotac omponentunt i l y out el l Angul arabouti t .
Tot el l Angul art hat0eroList+omponent i sac omponent ,at t ac hmet adat at ot hec l as s . I nT y peSc r i pt ,y ouat t ac hme t adat ab yus i ngadec or at or .Her e' ssomemet adat af or 0eroList+omponent: app/ her ol i s t . c omponent . t s( met adat a) @+omponent&{ module3d' module-id* selector' 1ero%list* templateUrl' 1ero%list-component-1tml* providers' ( 0ero4ervice ) }, export class 0eroList+omponent implements n3nit { !: - - - :! }
Her ei st he@+omponent dec or at or ,whi c hi dent i fi est hec l as si mmedi at el ybel owi t asac ompo ne ntc l as s . The@+omponent dec or at ort ak esar equi r edc onfi gur at i onobj ec twi t ht he i nf or mat i onAngul arneedst oc r eat eandpr es entt hec omponentandi t sv i ew. Her ear eaf ewoft hepos s i bl e@+omponent c onfi gur at i onopt i ons : •
et st hes our c eoft hebas eaddr es s( ormodul er el at i v eURLs module3d:s module-id)f s u c ha st h etemplateUrl.
•
el ec t ort hatt el l sAngul art oc r eat eandi ns er tani ns t anc eoft hi s selector:CSSs componentwher ei tfindsa61ero%list7 t agi npar entHTML.Fore x ampl e,i fanapp' s HTMLcont ai ns61ero%list76!1ero%list7,t henAngul ari ns er t sani ns t anc eoft he v i e w be t weent hos et ags . 0eroList+omponent
•
dul er el at i v eaddr es soft hi sc omponent ' sHTMLt empl at e,s hown templateUrl:mo above.
•
providers:a r r ayofdependenc yi nj ec t i onpr ov i der sf ors er v i c est hatt hec omponent
r equi r es .Thi si sonewayt ot el lAngul art hatt hec omponent ' scons t r uc t orr equi r esa 0ero4ervice soi tc angett hel i s tofher oest odi s pl ay .
Th eme t a dat ai nt he@+omponent t el l sAngul arwher et ogett hemaj orbui l di ng bl oc k sy ous pec i f yf ort hec omponent . Thet empl at e,me t a dat a,andc omponentt oget herdes c r i beav i e w. Appl yot hermet adat adec or at or si nas i mi l arf as hi ont ogui deAngul arbehav i or . d@utput ar eaf ewoft hemor epopul ardec or at or s . @3n=ectable,@3nput,an Thear c hi t ec t ur alt ak eawa yi st haty oumus taddmet adat at oy ourc odes ot hat An gu l a rk n owswh att odo .
Dat abi ndi ng Wi t houtaf r amewor k ,y ouwoul dber es pons i bl ef orpus hi ngdat av al uesi nt ot he HTMLc ont r ol sandt ur ni ngus err es pons esi nt oac t i onsandv al ueupdat es .Wr i t i ng s uc hpus h/ pul ll ogi cbyhandi st edi ous ,er r or pr one,andani ght mar et or eadasany e x per i enc edj Qu er ypr ogr ammerc anat t es t .
Angul ars uppor t sdat abi ndi ng,amec hani s mf orc oor di nat i ngpar t sofat empl at e wi t hpar t sofac omponent .Addbi ndi ngmar k upt ot het empl at eHTMLt ot el l Angul ar howt oc onnec tbot hs i des . Ast hedi agr am s hows ,t her ear ef ourf or msofdat abi ndi ngs y nt ax .Eac hf or m hasa di r ec t i on— t ot heDOM,f r om t heDOM,ori nbot hdi r ec t i ons . The0eroList+omponent exampl e t empl at ehast hr eef or ms : app/ her ol i s t . c omponent . ht ml ( bi ndi ng) 6li7{{1ero-name}}6!li7 61ero%detail (1ero)5
•
The{{1ero-name}} i nt er pol at i on di s pl a y st hec omponent ' s1ero-name pr oper t yval ue wi t hi nt he6li7 t ags .
•
The(1ero) pr oper t ybi ndi ng pas s est hev al ueofselected0ero f r om t hepar ent 0eroList+omponent t ot he1ero pr oper t yoft hec hi l d0ero.etail+omponent.
•
The&clic9, ev entbi ndi ng c al l st hec omponent ' sselect0ero met hodwhent heuser c l i c k saher o' sname. T wowa ydat abi ndi ngi sani mpor t antf our t hf or mt hatc ombi nespr oper t yandev ent bi ndi ngi nas i ngl enot at i on,us i ngt hengModel di r ec t i v e.Her e' sanex ampl ef r om t he t empl at e: 0ero.etail+omponent
app/ her odet ai l . c omponent . ht ml ( ngModel ) 6input (&ngModel,)5<1ero-name<7
I nt wowa ybi ndi ng,adat apr oper t yv al uefl owst ot hei nputbo xf r om t hec omponent aswi t hpr oper t ybi ndi ng.Theus er ' sc hangesal s ofl owbac kt ot hec omponent , r es et t i ngt hepr oper t yt ot hel at es tv al ue,aswi t hev entbi ndi ng. Angul arpr oc es s esal l dat abi ndi ngsonc eperJ av aSc r i ptev entc y cl e,f r om t her ootof t heappl i c at i onc omponentt r eet hr oughal l c hi l dc omponent s .
Dat abi ndi ngpl a y sani mpor t antr ol ei nc ommuni c at i onbet weenat empl at eandi t s component .
Dat abi ndi ngi sal s oi mpor t antf orc ommuni c at i onbet weenpar entandc hi l d component s.
Di r ec t i v es
An gu l a rt emp l at esar ed y nami c .Wh enAn gul arr e nd er st h em,i tt r a ns f o r mst h eDOM ac cor di ngt ot hei ns t r uc t i onsgi v enbydi r ec t i v es . Adi r ec t i v ei sac l as swi t hdi r ec t i v emet adat a.I nTy peSc r i pt ,appl yt
[email protected] dec or at ort oat t ac hmet adat at ot hec l as s.
Ac omponenti sadi r ec t i v ewi t hat empl at e;a@+omponent dec or at ori sac t ual l ya ec or at orex t endedwi t ht empl at eor i ent edf eat ur es . @.irective d Whi l eac omponenti st ec hni c al l yadi r ec t i v e,c omponent sar es odi s t i nc t i v eand c ent r al t oAngul arappl i c at i onst hatt hi sar c hi t ec t ur al ov er v i ews epar at escomponent s f r om di r ec t i v es . Twoot herk i ndsofdi r ec t i v esex i s t :s t r uc t ur al andat t r i but edi r ec t i v es . The yt endt oappearwi t hi nanel ementt agasat t r i but esdo,s ome t i mesbynamebut mor eof t enast het ar getofanas s i gnmentorabi ndi ng. St r uc t ur al di r ec t i v esal t erl ay outbyaddi ng,r emov i ng,andr epl ac i ngel ement si n DOM. Thee x ampl et empl at e us est wobui l t i ns t r uc t ur al di r ec t i v es :
app/ her ol i s t . c omponent . ht ml ( s t r uc t ur al ) 6li :ng;or5
• •
:ng;or t el l sAngul art os t ampoutone6li7 perher oi nt he1eroes l i s t . :ng3f i nc l udest he0ero.etail c omponentonl yi fas el ec t edher oex i s t s .
At t r i but edi r ec t i v esal t ert heappear anc eorbehav i orofanex i s t i ngel ement .I n t empl at est heyl ookl i k er egul arHTMLat t r i but es ,henc et hename. ThengModel di r ec t i v e,whi c hi mpl ement st wowaydat abi ndi ng,i sanex ampl eofan at t r i but edi r ec t i v e.ngModel modi fi est hebehav i orofanex i s t i ngel ement( t y pi c al l yan yset t i ngi t sdi s pl ayval uepr oper t yandr es pondi ngt oc hangeev ent s . 6input7)b app/ her odet ai l . c omponent . ht ml ( ngModel ) 6input (&ngModel,)5<1ero-name<7
Angul arhasaf ewmor edi r ec t i v est hatei t heral t ert hel ay outs t r uc t ur e( f orex ampl e, n gSwi t c h)ormo di f ya s pe c t so fDOM e l eme nt sa ndc omp on ent s( f o re x amp l e , ngSt y l e andngCl ass) . Ofc our s e,y o uc anal s owr i t ey o uro wndi r ec t i v e s .Component ss uc has ar eonek i ndofc us t om di r ec t i v e. 0eroList+omponent
Ser v i c es
Ser v i c ei sabr oadc at egor yenc ompas s i nganyval ue,f unc t i on,orf eat ur et haty our appl i c at i onneeds . Al mos tany t hi ngc anbeas er v i c e.As er v i c ei st y pi c al l yac l as swi t hanar r ow,wel l -
defi nedpur pos e.I ts houl ddos omet hi ngs pec i fi canddoi twel l .
Ex ampl esi nc l ude: 1
l oggi ngs er v i c e
2
dat as er v i c e
3
mes s agebus
4
t axc al c ul at or
5
appl i cat i onconfi gur at i on Ther ei snot hi ngs pec i fi cal l yAngul arabouts er v i c es .Angul arhasnodefi ni t i onofa s er v i c e.Ther ei snos er v i c ebas ec l as s,andnopl ac et or egi s t eras er v i c e. Yets er v i c esar ef undament al t oan yAngul arappl i c at i on.Component sar ebi g c ons umer sofs er v i c es . Her e' sanex ampl eofas er v i c ec l as st hatl ogst ot hebr ows erc ons ol e: app/ l ogger . s er v i c e. t s( c l as s ) export class Logger { log&msg' an/, { console-log&msg," } error&msg' an/, { console-error&msg," } $arn&msg' an/, { console-$arn&msg," } }
Her e' sa0ero4ervice t hatf et c hesher oesandr et ur nst hem i nar es ol v edPr omi se. The0ero4ervice dependsont heLogger s er v i c eandanot her#ac9end4ervice t hat handl est heser v erc ommuni c at i ongr untwor k . app/ her o. s er v i c e. t s( c l as s) export class 0ero4ervice { private 1eroes' 0ero() 5 ()"
constructor& private bac9end' #ac9end4ervice* private logger' Logger, { }
get0eroes&, { t1is-bac9end-getAll&0ero,-t1en& &1eroes' 0ero(), 57 { t1is-logger-log&>;etc1ed ?{1eroes-lengt1} 1eroes->," t1is-1eroes-pus1&---1eroes," !! ll cac1e }," return t1is-1eroes" } }
Ser v i c esar eev er y wher e. Componentc l as sess houl dbel ean.Theydon' tf et c hdat af r om t hes er v er ,v al i dat e us eri nput ,orl ogdi r ec t l yt ot hec ons ol e.Theydel egat es uc ht as kst os er v i c es . Ac omponent ' sj obi st oenabl et heus ere xper i enc eandnot hi ngmor e.I tmedi at es bet weent hev i ew( r ender edbyt het empl at e)andt heappl i c at i onl ogi c( whi c hof t en i nc l udess omeno t i onofamodel ) .Ag oodcompo nentpr es ent spr oper t i esand met hodsf ordat abi ndi ng.I tdel egat esev er y t hi ngnont r i v i al t os er v i c es . Angul ardoes n' tenf or c et hes epr i nc i pl es .I twon' tc ompl ai ni fy ouwr i t ea" k i t c hen s i nk "c omponentwi t h3000l i nes . Angul ardoeshel py ouf ol l owt hes epr i nc i pl esbymak i ngi teas yt of ac t ory our appl i c at i onl ogi ci nt os er v i c esandmak et hos es er v i c esav ai l abl et oc omponent s t hr oughdependenc yi nj ec t i on.
Dependenc yi nj ec t i on
Dependenc yi nj ec t i oni sawayt os uppl yanewi ns t anc eofac l as swi t ht hef ul l y -
f or meddependenc i esi tr equi r es .Mos tdependenc i esar es er v i c es .Angul arus es dependenc yi nj ec t i ont opr o vi denewc omponent swi t ht hes er v i c est he yneed.
Angul arc ant el lwhi c hs er v i c esac omponentneedsbyl ook i ngatt het y pesofi t s c ons t r uc t orpar amet er s .Forex ampl e,t hec ons t r uc t orofy our0eroList+omponent needsa0ero4ervice: app/ her ol i s t . c omponent . t s( c ons t r uc t or ) constructor&private service' 0ero4ervice, { }
WhenAngul arc r eat esac omponent ,i tfi r s tas ksani nj ec t orf ort hes er v i c est hatt he c ompo nentr equi r es . Ani nj ec t ormai nt ai nsac ont ai nerofs er v i c ei ns t anc est hati thaspr ev i ous l yc r eat ed. I far eques t eds er v i c ei ns t anc ei snoti nt hec ont ai ner ,t hei nj ec t ormak esoneand addsi tt ot hec ont ai nerbef or er et ur ni ngt hes er v i c et oAngul ar .Whenal l r eques t ed s er v i c esha v ebeenr es ol v edandr et ur ned,Angul arc anc al lt hec omponent ' s c ons t r uc t orwi t ht hos es er v i c esasar gument s .Thi si sdependenc yi nj ec t i on. Th epr o c es so f0ero4ervice i nj ec t i onl ook sabi tl i k et hi s :
I ft hei nj ec t ordoes n' thav ea0ero4ervice,how doesi tknowhow t omakeone?
I nbr i ef ,y oumus thav epr ev i ous l yr egi s t er edapr ov i deroft he0ero4ervice wi t ht he i nj ec t or .Apr ov i deri ss omet hi ngt hatc anc r eat eorr et ur nas er v i c e,t y pi c al l yt he s er v i c ec l as si t s el f . Youc anr egi s t erpr o vi der si nmodul esori nc omponent s . I ngener al ,addpr ov i der st ot her oo tmodul e s ot hatt hes amei ns t anc eofas er v i c ei s av ai l abl eev er y wher e. app/ app. modul e. t s( modul epr o vi der s ) providers' ( #ac9end4ervice* 0ero4ervice* Logger )*
Al t er nat i v el y ,r egi s t eratac omponentl ev eli nt heproviders pr oper t yoft he @+omponent me t adat a: app/ her ol i s t . c omponent . t s( c omponentpr ov i der s ) @+omponent&{ module3d' module-id* selector' 1ero%list* templateUrl' 1ero%list-component-1tml* providers' ( 0ero4ervice ) },
Regi s t er i ngatac omponentl e v elmeansy ougetanewi ns t anc eoft hes er v i c ewi t h eac hne wi ns t anc eoft hatc omponent . Poi nt st or ememberaboutdependenc yi nj ec t i on: 1
Dependencyi nj ect i oni swi r edi nt ot heAngul arf r amewor kandusedever ywher e.
2
Thei nj ect ori st hemai nmechani sm. 1 Ani nj ec t ormai nt ai nsac ont ai nerofs er v i c ei ns t anc est hati tc r eat ed.
2 Ani nj ec t orc anc r eat eanews er v i c ei ns t anc ef r om apr ov i der . 3
Apr o v i de ri sar e c i p ef o rc r e at i ngas e r v i c e .
4
Reg i s t e rp r o v i d er swi t hi nj e c t o r s .
Wr apup You' v el ear nedt hebas i c saboutt heei ghtmai nbui l di ngbl oc k sofanAngul ar appl i c at i on: 1
Modul es
2
Component s
3
T emp l a t e s
4
Met adat a
5
Dat abi ndi ng
6
Di r ec t i v es
7
Ser v i c es
8
Dependenc yi nj ec t i on That ' saf oundat i onf orev er y t hi ngel s ei nanAngul arappl i c at i on,andi t ' smor et han enought ogetgoi ng.Buti tdoes n' ti nc l udeev er y t hi ngy ouneedt ok now. Her ei sabr i ef ,al phabet i c all i s tofot heri mpor t antAngul arf eat ur esands er v i c es . Mos toft hem ar ec ov er edi nt hi sdoc ument at i on( ors oonwi l lbe) . Ani mat i ons :An i mat ec o mpo ne ntb eh av i o rwi t h ou tde epk n owl e dg eo fa ni ma t i o n t ec hni quesorCSSwi t hAngul ar ' sani mat i onl i br ar y . Changede t ec t i on:Thec hangede t ec t i ondoc ument at i onwi l l c o v erho w Angul ar dec i dest hatac omponentpr oper t yv a l uehasc hanged,whent oupdat et hes c r een, andhowi tus esz onest oi nt er c eptas y nc hr onousac t i v i t yandr uni t sc hange det ec t i onst r at egi es .
Ev e nt s :Theev e nt sd oc ument at i onwi l lc o v erho wt ous ec ompone nt sands er v i c es t or ai s eev ent swi t hmec hani s msf orpubl i s hi ngands ubs c r i bi ngt oev ent s . For ms :Suppor tc ompl e xdat aent r ys c enar i oswi t hHTML bas edv al i dat i onanddi r t y c hec ki ng. HTTP:Communi c at ewi t has er v ert ogetdat a,s av edat a,andi nv ok es er v er s i de ac t i onswi t hanHTTPc l i ent . Li f ec y cl ehook s:T api nt ok eymoment si nt hel i f et i meofac omponent ,f r om i t s c r eat i ont oi t sdes t r uc t i on,byi mpl ement i ngt hel i f ec y cl ehooki nt er f ac es . Pi p es :Us epi pesi ny ourt empl at est oi mpr ov et heus ere xper i enc ebyt r ans f or mi ng v al uesf ordi s pl ay .Cons i dert hi scurrenc/ pi peex pr es si on: price currenc/'U4.'true
I tdi s pl ay sapr i c eof" 42. 33"as?B2-CC. Rout er :Nav i gat ef r om paget opagewi t hi nt hec l i entappl i c at i onandnev erl eav et he br ows er . Tes t i ng:Rununi tt es t sony ourappl i c at i onpar t sast heyi nt er ac twi t ht heAngul ar f r amewor kus i ngt heAngul arT es t i ngPl at f or m.
Templ at eSynt ax .
Ou rAngul arappl i c at i onmana geswhatt heus ers eesandc ando,ac hi e v i ngt hi s t hr ought hei nt er ac t i onofaComponentc l as si ns t anc e( t hec omponent )andi t sus er f ac i ngt empl at e. Manyofusar ef ami l i arwi t ht hec omponent / t empl at edual i t yf r om oure xper i enc ewi t h model v i ewc ont r ol l er( MVC)ormodel v i ewv i ewmodel ( MVVM) .I nAngul ar ,t he c omponentpl ay st hepar toft hec ont r ol l er / v i ewmodel ,andt het empl at er epr es ent s t hev i ew. Let ’ sfindoutwhati tt ak est owr i t eat empl at ef orourv i ew.We’ l lc ov ert hes ebas i c el ement soft empl at es y nt ax : •
HTML
•
I nt er pol at i on
•
T empl at ee x pr es s i ons
•
T empl at es t at ement s
•
Bi ndi ngs y nt ax
•
Pr oper t ybi ndi ng
•
At t r i but e,c l as s ,ands t y l ebi ndi ngs
•
Ev entbi ndi ng
•
T wowa ydat abi ndi ng
•
T wowa ydat abi ndi ngwi t hNgModel
•
Bui l t i ndi r ec t i v es NgCl ass NgSt y l e NgI f NgSwi t ch NgFor
•
*and
•
T empl at er ef er enc ev ar i abl es
•
I nputandout putpr oper t i es
•
T empl at ee xpr es s i onoper at or s pi pe s af enav i gat i onoper at or( ?. ) Thel i v eex ampl e demons t r at esal l oft hes ynt axandc odes ni ppet sdes cr i bedi nt hi s c h a p t e r .
HTML HTMLi st hel anguag eoft heAngul art empl at e.Th eQui c k St ar t appl i c at i onhasa t empl at et hati spur eHTML: 61D70ello Angular6!1D7
Al mos tal lHTMLs y nt axi sv al i dt empl at es ynt ax .The6script7 el ementi sanot abl e ex cept i on;i ti sf or bi dden,el i mi nat i ngt her i s kofs c r i pti nj ec t i onat t ac k s .( I npr ac t i c e, i ssi mpl yi gnor ed. ) 6script7 Somel egalHTMLdoesn’ tmakemuchsensei nat empl at e.The61tml7,6bod/7, and6base7 el ement shav enous ef ulr ol ei nourr eper t oi r e.Pr et t ymuc hev er y t hi ng el s ei sf ai rgame. Wecanext endt heHTMLvocabul ar yofourt empl at eswi t hcomponent sand di r ec t i v est hatappearasnewel ement sandat t r i but es .I nt hef ol l owi ngs ec t i onswe ar ego i n gt ol ea r nho wt oge ta nds etDOM ( Do c umen tOb j ec tMo de l )v a l u es dy nami c al l yt hr oughdat abi ndi ng. Let ’ st ur nt ot hefi r s tf or m ofdat abi ndi ng— i nt er pol at i on— t os eehowmuc hr i c her t empl at eHTMLcanbe.
I nt er pol at i on Wemett hedoubl ec ur l ybr ac esofi nt er pol at i on,{{ and}},ear l yi nourAngul ar educ at i on.
6p7M/ current 1ero is {{current0ero-rstName}}6!p7
Weus ei nt er pol at i ont oweav ec al c ul at eds t r i ngsi nt ot het ex tbet weenHTML el ementt agsandwi t hi nat t r i but eas si gnment s . 61C7 {{title}} 6img src5<{{1ero3mageUrl}}< st/le5<1eig1t'CEpx<7 6!1C7
Themat er i al be t weent hebr ac esi sof t ent henameofac omponentpr oper t y . Angul arr epl ac est hatnamewi t ht hes t r i ngv al ueoft hec or r es pondi ngc omponent pr oper t y .I nt heex ampl eabov e,Angul arev al uat est hetitle and1ero3mageUrl pr oper t i esand" fi l l si nt hebl ank s " ,fi r s tdi s pl ay i ngabol dappl i c at i ont i t l eandt hena her oi ci mage. Mor egener al l y ,t hemat er i al bet weent hebr ac esi sat empl at eex pr es si ont hat Angul arfi r s tev al uat esandt henc onv er t st oas t r i ng.Thef ol l owi ngi nt er pol at i on i l l us t r at est hepoi ntbyaddi ngt het wonumber swi t hi nbr ac es : 6F%% <1e sum of D H D is 2< %%7 6p7 1e sum of D H D is {{D H D}}6!p7
Th ee x pr e s s i onc a ni n v ok eme t h od so ft heh os tc o mpo nen t ,aswed ohe r ewi t h getIal&,: 6F%% <1e sum of D H D is not B< %%7 6p7 1e sum of D H D is not {{D H D H getIal&,}}6!p7
Angul arev al uat esal lex pr es si onsi ndoubl ec ur l ybr ac es ,c onv er t st heex pr es si on r es ul t st os t r i ngs ,andl i nk st hem wi t hnei ghbor i ngl i t er als t r i ngs .Fi nal l y ,i tas s i gns t hi sc ompos i t ei nt er pol at edr es ul tt oanel ementordi r ec t i v epr oper t y . Weappeart obei ns er t i ngt her es ul tbet weenel ementt agsandas s i gni ngi tt o at t r i but es .I t ' sconv eni entt ot hi nkso,andwer ar el ysufferf ort hi smi s t ak e.Though t hi si snotex ac t l yt r ue.I nt er pol at i oni sas pec i als y nt axt hatAngul arc onv er t si nt oa pr oper t ybi ndi ng,andi sex pl ai nedbel ow. Butfi r s tl et ' st ak eac l os erl ookatt empl at eex pr es si onsands t at ement s .
T empl at ee xpr es s i ons At empl at eex pr es s i onpr oduc esav al ue.Angul are x ec ut est heex pr es s i onand as s i gnsi tt oapr oper t yofabi ndi ngt ar get ;t het ar getmi ghtbeanHTMLel ement ,a c omponent ,oradi r ec t i v e. Weputat empl at ee xpr es s i onwi t hi nt hei nt er pol at i onbr ac eswhenwewr ot e{{D H ’ l ls eet empl at eex pr es si onsagai ni nt hepr oper t ybi ndi ng s ec t i on,appear i ng D}}.We i nquot est ot her i ghtoft he5 s y mbo la si n(propert/)5
5, H5,%5,. as s i gnment s( . . ) ne$
•
chai ni nge x pr ess i onswi t h" or*
•
HH i nc r ementanddec r ementoper at or s( and%%)
Ot hernot abl edi ffer enc esf r om J av aSc r i pts y nt axi nc l ude: •
nosuppor tf ort hebi t wi seoper at or s andJ
•
newt empl at ee xpr es s i onoper at or s ,s uc has andK-
Ex pr es si oncont ex t Per hapsmor es ur pr i s i ng,t empl at eex pr es si onscannotr ef ert oany t hi ngi nt hegl obal names pac e.Theyc an’ tr ef ert o$indo$ ordocument.Theyc an’ tc al l r console-log o yar er es t r i c t edt or ef er enc i ngmember soft heex pr es si onc ont ex t . Mat1-max.The Theex pr es si onc ont ex ti st y pi c al l yt hec omponenti ns t anc e,whi c hi st hes our c eof bi ndi ngv al ues .
Whenwes eet i t l ewr appedi ndoubl ec ur l ybr ac es ,{{title}} ,wek n owt ha ttitle i sa pr oper t yoft hedat aboundcomponent .Whenweseei sUnchangedi n nowwear er ef er r i ngt ot hatc omponent ' s (disabled)5
Ex pr es si ongui del i nes T empl at ee xpr es s i onsc anmak eorbr eakanappl i c at i on.Pl eas ef ol l owt hes e gui del i nes : •
Nov i s i bl es i deeffec t s
•
Qui c kex ec ut i on
•
Si mpl i c i t y
•
I dempot ence Theonl yex c ept i onst ot hes egui del i ness houl dbei ns pec i fi cci r c ums t anc est haty ou t hor oughl yunder s t and. NO VI SI BLESI DEEFFECTS At empl at eex pr es s i ons houl dnotc hangeanyappl i c at i ons t at eot hert hant hev al ue oft het ar getpr oper t y . Thi sr ul ei ses sent i al t oAngul ar ' s" uni di r ec t i onal dat afl ow"pol i c y .Wes houl dnev er wor r yt hatr eadi ngac omponentv a l uemi ghtc hanges omeo t herd i s pl a y edv a l ue. Thev i ews houl dbes t abl et hr oughoutas i ngl er ender i ngpas s . QUI CKEXECUTI ON
Angul are x ec ut est empl at ee xpr es s i onsmo mor eof t ent hanwet hi nk .The yc anbec al l ed af t ere v er yk ey pr es sormous emov e.Ex pr es s i onss houl dfi ni s hqui c k l yort heus er e xper i enc ema ydr ag,es pec i al l yons l owerdev i c es .Cons i derc ac hi ngv al ues c omput edf r om o t herv a l u eswhent hec omput at i oni se xpens i v e . SI MPLI CI TY Al t houghi t ' spos si bl et owr i t equi t ec ompl ext empl at eex pr es si ons ,wer eal l y s houl dn' t . Apr o pe r t yna me meo rme t h odc a l l s h ou l db et h en or m.Ano c c as i o nal Bo ol e an F)i negat i on( sOK.Ot her wi s e,c onfi neappl i c at i onandbus i nes sl ogi ct ot he
c omponenti t s el f ,wher ei twi l l beeas i ert odev el opandt es t . I DEMP MPOTENCE Ani dempot ent ex pr es si oni si deal bec aus ei ti sf r eeofs i deeffec t sandi mpr ov es Angul ar ' sc hangedet ec t i onper f or manc e. I nAngul art er ms ,ani dempot entex pr es s i onal wa ysr et ur nsex ac t l yt hes amet hi ng unt i l oneofi t sdependentv al ueschanges . Dependentv al uess houl dnotc hangedur i ngas i ngl et ur noft heev entl oop.I fan i dempot entex pr es si onr et ur nsas t r i ngoranumber ,i tr et ur nst hes ames t r i ngor numberwhenc al l edt wi c ei nar ow.I ft heex pr es si onr et ur nsanobj ec t( i nc l udi ngan Arra/) ,i tr et ur nst hes ameobj ec tr ef er enc ewhenc al l edt wi c ei nar ow.
T empl at es t at ement s At empl at es t at ementr es pondst oane v entr ai s edb yabi ndi ngt ar gets uc hasan el ement ,c omponent ,ordi r ec t i v e. We’ l ls eet empl at es t at ement si nt heev entbi ndi ng sec t i on,appear i ngi nquot est o t her i ghtoft he5 s y mb mb ol a si n&event,5
At empl at es t at ementhasas i deeffec t .I t ' showweupdat eappl i c at i ons t at ef r om us eri nput .Ther ewoul dbenopoi ntt or es pondi ngt oanev entot her wi s e. Res pondi ngt oev ent si st heot hers i deofAngul ar ' s" uni di r ec t i onaldat afl ow" .We' r e f r eet oc hangeany t hi ng,any wher e,dur i ngt hi st ur noft heev entl oop. Li k et empl at ee xpr es s i ons ,t empl at es t at ement sus eal anguaget hatl ook sl i k e J av aSc r i pt .Thet empl at es t at ementpar s eri sdi ffer entt hant het empl at eex pr es si on par s erands pec i fi c al l ys uppor t sbot hbas i cas s i gnment( ndc hai ni ngex pr es s i ons 5)a ( wi t h" or*) . Howev er ,c er t ai nJ av aSc r i pts ynt axi snotal l owed: •
ne$
•
i ncr eme mentanddec r eme mentoper at or s ,HH and%%
•
oper at oras s i gnment ,s uc hasH5 and%5
•
t hebi t wi s eoper at or s andJ
•
t het empl at ee xpr es s i onoper at or s
St at ementc ont ex t Aswi t hex pr es s i ons ,s t at ement sc anr ef eronl yt owhat ' si nt hes t at ementc ont ex t— t y pi c al l yt hec omponenti ns t anc et owhi c hwe' r ebi ndi ngt heev ent . T empl at es t at ement sc annotr ef ert oany t hi ngi nt hegl obalnames pac e.Theyc an’ t console-log o r ef ert o$indo$ ordocument.Theyc an’ tc al l rMat1-max.
TheonSav ei n&clic9,5
St at ementgui del i nes Aswi t hex pr es s i ons ,av oi dwr i t i ngc ompl e xt empl at es t at ement s .Amet hodc al lor s i mpl epr oper t yas s i gnments houl dbet henor m. Nowt hatweha v eaf eelf ort empl at ee xpr es s i onsands t at ement s ,we ’ r er eadyt o l ear naboutt hev ar i et i esofdat abi ndi ngs ynt axbey ondi nt er pol at i on.
Bi ndi ngs y nt ax :Anov er v i ew Dat abi ndi ngi samec hani s mf orc oor di nat i ngwhatus er ss eewi t happl i c at i ondat a v al ues .Whi l ewec oul dpus hv al uest oandpul lv al uesf r om HTML,t heappl i c at i oni s eas i ert owr i t e,r ead,andmai nt ai ni fwet ur nt hes ec hor esov ert oabi ndi ng f r a me me wor k .Wes i mp l yde c l a r eb i n di ng sbe t wee nbi nd i n gs o ur c e sa ndt a r ge tHTML el ement sandl e tt hef r amewor kdot hewor k . Angul arpr ov i desma manyki ndsofdat abi ndi ng,andwe’ l ldi s c us seac hoft hem i nt hi s c hapt er .Fi r s twe' l l t ak eahi ghl ev elv i ewofAngul ardat abi ndi ngandi t ssy nt ax . Wec angr oupal l bi ndi ngsi nt ot hr eec at egor i esbyt hedi r ec t i oni nwhi c hdat afl ows . Eac hc at egor yhasi t sdi s t i nc t i v es ynt ax :
Dat a di r ec t i on
Oneway f r om d at a s o u r c e
Synt ax
{{expression}} (target) 5
Bi ndi ng t y p e
I nt er pol at i on Pr oper t y
t ov i ew
At t r i but e
t ar get
Cl a s s