www.it-ebooks.info Summary of Contents Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii 1. Falling In Love With AngularJS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 2. Modules, Controllers & Data Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 3. AngularJS Scope & Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 4. Multiple Views and Routing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 5. AngularJS Services, Factories, and Providers . . . . . . . . . . . . . . . . . . . . . . . . . 99 6. Developing Single Page Blogger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 7. Understanding AngularJS Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 8. Interacting with REST APIs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151 9. Using REST APIs in Single Page Blogger . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179 10. AngularJS Directives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193 11. Adding a Comment System to Single Page Blogger . . . . . . . . . . . . . . . . . 219 12. Dependency Injection In AngularJS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225 13. AngularJS Filters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233 14. AngularJS Animation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245 15. Deployment and Internationalization . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259 16. Authentication and Authorization in AngularJS . . . . . . . . . . . . . . . . . . . . 271 www.it-ebooks.info www.it-ebooks.info ANGULARJS: NOVICE TO NINJA BY SANDEEP PANDA www.it-ebooks.info iv AngularJS: Novice to Ninja bySandeepPanda Copyright©2014SitePointPty.Ltd. Product Manager:SimonMackie English Editor:PaulFitzpatrick Technical Editor:GoloRoden Cover Designer:AlexWalker Notice of Rights Allrightsreserved.Nopartofthisbookmaybereproduced,storedinaretrievalsystemortransmitted inanyformorbyanymeans,withoutthepriorwrittenpermissionofthepublisher,exceptinthecase ofbriefquotationsembodiedincriticalarticlesorreviews. Notice of Liability Theauthorandpublisherhavemadeeveryefforttoensuretheaccuracyoftheinformationherein. However,theinformationcontainedinthisbookissoldwithoutwarranty,eitherexpressorimplied. NeithertheauthorsandSitePointPty.Ltd.,noritsdealersordistributorswillbeheldliableforany damagestobecausedeitherdirectlyorindirectlybytheinstructionscontainedinthisbook,orbythe softwareorhardwareproductsdescribedherein. Trademark Notice Ratherthanindicatingeveryoccurrenceofatrademarkednameassuch,thisbookusesthenamesonly inaneditorialfashionandtothebenefitofthetrademarkownerwithnointentionofinfringementof thetrademark. PublishedbySitePointPty.Ltd. 48CambridgeStreetCollingwood VICAustralia3066 Web:www.sitepoint.com Email:[email protected] ISBN978-0-9922794-5-5(print) ISBN978-0-9924612-6-3(ebook) PrintedandboundintheUnitedStatesofAmerica www.it-ebooks.info v About Sandeep Panda SandeepPandaisawebdeveloperandwriterwithapassionforJavaScriptandHTML5.He hasoverfouryears'experienceprogrammingfortheWeb.Helovesexperimentingwithnew technologiesastheyemergeandisacontinuouslearner.Whilenotprogramming,Sandeep canbefoundplayinggamesandlisteningtomusic. About SitePoint SitePointspecializesinpublishingfun,practical,andeasy-to-understandcontentforweb professionals.Visithttp://www.sitepoint.com/toaccessourblogs,books,newsletters,articles, andcommunityforums.You’llfindastackofinformationonJavaScript,PHP,Ruby,mobile development,design,andmore. www.it-ebooks.info www.it-ebooks.info TomyMomandDadwhotaught metolovebooks.It'snotpossible tothankyouadequatelyfor everythingyouhavedoneforme. Tomygrandparentsfortheir strongsupport.Tomybrother Preetishforbeingaconstant sourceofinspiration.Andtomy awesomefriendsIpseetaand Fazleforalwaysbelievinginme. www.it-ebooks.info www.it-ebooks.info Table of Contents Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii Who Should Read This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii Conventions Used . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xviii Code Samples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xviii Tips, Notes, and Warnings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix Supplementary Materials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix Want to Take Your Learning Further? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xx Chapter 1 Falling In Love With AngularJS . . . . . 1 The Power Features of AngularJS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Download and Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Installing via CDN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Hosting on Your Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Required Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 AngularJS Batarang . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 The Angular Seed Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 The Anatomy of an AngularJS app . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 What is MVW? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Structuring Our Code With MVC . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Unit and End-to-End Testing in AngularJS . . . . . . . . . . . . . . . . . . . . . . . . 17 Where to Put Your Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 How to Run Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 When Not To Use AngularJS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 www.it-ebooks.info