In recent years, a modern approach for building client-centric applications, Single-Page Web Applications, has become well-known in our developer community. As a result, there are several new frameworks that have made their way to the professional developer’s landscape.
AngularJS is a new framework that radically changes the way developers build a web application. It is one of the most ideal alternatives that offers a powerful foundation to build your web application. This slides is dedicated to developers who want to learn the best practices in single-page approaches and how to apply them with AngularJS.
7. IS SPA THE FUTURE OF WEB APP ?
(one of) SPA pitfall: content-heavy sites
Highlight SPA apps
8. SPA BENEFITS
Runs faster
Gives better UX
Is able to operate offline (thanks to HTML5)
Uses less network bandwidth
9. SPA DRAWBACKS
Site content becomes SEO-unfriendly
Insecure application code
Aware navigation browser buttons: Back,
Forward and Reload
Run well in browsers compatible with HTML5
History API
13. WHY ANGULARJS
First release v0.9.0 on Oct, 2010
Maintained by Google and community
Active development: 03 new releases per
month
MIT license (like jQuery)
(shameless ad -:) Many killer features
18. EXPLORE THE CODE
Initialize AngularJS
Controllers, Views and Routes
Two-way binding in Model and
View
Interaction between View and
Controller
22. ANGULARJS INJECTOR SERVICE
Injector is responsible for creating two types of
objects:
User-defined Objects:
– We register a “recipe” to instruct Injector in how
to create objects
– Recipe types: Value, Factory, Service, Provider
and Constant
Specialized Objects: Controllers, Directives,
Filters and Animations
23. INJECT DEPENDENCY IN ANGULARJS
Elements to have dependencies:
- Controller, Directive, Filter and
Animation
- Factory, Service and Provider
Elements can be injected (recipe):
Service, Provider, Value, Factory
and Constant
26. DIRECTIVES
are markers on a DOM element (an attribute or an
element name)
attach behaviors to that DOM element and/or
transform the DOM element (and its children)
Appeared sometime in 2005Show the point: the page does not reload after loaded first timeIn an SPA, either all necessary code – HTML, JavaScript, and CSS – is retrieved with a single page load, or the appropriate resources are dynamically loaded and added to the page as necessary, usually in response to user actions. The page does not reload at any point in the process, nor does control transfer to another page, although modern web technologies (such as those included in HTML5) can provide the perception and navigability of separate logical pages in the application. Interaction with the single page application often involves dynamic communication with the web server behind the scenes.
SPA is good for web application or game such as management (todo list, tasks, projects), utilities (rss readers, email), education, etc.Content-heavy sites: blog, newspaper, magazine, etc.
Site content becomes SEO-unfriendly HTML is generated by javascript and crawler has no way to read itInsecure application code business code is exposed to clientAware navigation browser buttons: Back, Forward and Reload handle when user clicks on these buttonsHTML5 History API: although SPA frameworks can runs ok in non-History API browsers but this limits some features e.g. can’t handle bookmarked links
route /raʊt/In angularjs, View here refers as a templateRoute is to link a View and a Controller togetherModel and View (template) are compiled to make final HTML document for end-user (with attached events if any)Two-way binding: any changes on Model is detected automatically and re-compiles
Initialize AngularHow static HTML looks likeHow to bootstrap app with AngularHow to define and load dependent angular modulesControllers, Views and RoutesHow to setup routesController is factory method$scope is data-model or execution contextTwo-way binding in Model and ViewChanges in model are compiled to viewOnly detect changes in “angular” runtime (update scope in settimeout doesn’t work)Interaction between View and Controller$scope is central access to interact
HTTP service here isn’t jQueryajax, it’s ajaximplementation in AngularJS
Introduce definition of dependency injectionExplain sample code: one approach to inject dependency. Here it removes the responsibility of locating the dependency from the component.SomeClass is not concerned with locating the greeter dependency, it is simply handed the greeter at runtimeIt puts the responsibility of getting hold of the dependency on the code that constructs SomeClass, in AngularJS, there is a service called Injector to take this responsibility
Injector will create and cache User-defined ObjectsThe most verbose, but also the most comprehensive one is a Provider recipe. The remaining four recipe types — Value, Factory, Service and Constant — are just syntactic sugar on top of a provider recipe
How to inject dependencies to elements
Not all recipes have demo code, only: value, factory, serviceService vs. Factory: service is created with new operator