36
Angular.JS Advanced Angular.JS - GDayX VN 2013

Nicolas Embleton, Advanced Angular JS

Embed Size (px)

DESCRIPTION

Angular.JS is a modern Javascript MVC Framework that was built from the ground up by a team of Googlers, sponsored by Google itself. Angular.JS allows web developers a clear separation between logic and view, and greatly improves the ability to reuse the code by using things such as Directives, Services, Components.Angular.JS smart templating engine also allows to minimize the HTML code, During the presentation, you'll learn some medium-advanced usages of Angular.JS, how to use it, tips & tricks that will make your app amazing.

Citation preview

Page 1: Nicolas Embleton, Advanced Angular JS

Angular.JSAdvanced Angular.JS - GDayX VN 2013

Page 2: Nicolas Embleton, Advanced Angular JS

About me

Nicolas Embleton, French in Ho Chi Minh City● 2005 - Software Engineer and System Architect, working on legacy tech

stacks (C++, OpenGL, Java, ...) then quickly Web (PHP)● 2009 - Founded a Mobile Development Company of 30 individuals● 2011 - Started Datafield Startup, Co-founder, CTO● 2013 - Started the Javascript Ho Chi Minh City meetup, Getting active in

Startup Vietnamese scene to support and mentor young talents

Page 3: Nicolas Embleton, Advanced Angular JS

Agenda

● Quick Intro● Bootstrapping● Why Angular?● Main features, and why it's awesome● Best practices● Testing, tooling● And SEO?● Final words

Page 4: Nicolas Embleton, Advanced Angular JS

Intro (quick)From Wikipedia:

AngularJS is built around the belief that declarative programming should be used for building UIs

and wiring software components, while imperative programming is excellent for

expressing business logic. The framework adapts and extends traditional HTML to better serve dynamic

content through two-way data-binding that allows for the automatic synchronization of models and views. As a result,

AngularJS deemphasizes DOM manipulation and improves testability.

Page 5: Nicolas Embleton, Advanced Angular JS

Angular JS quick review

● Templating● Model View Controller (MVC)● Extends HTML (very flexible)● 2-ways Data-binding ● Very reusable (if you follow best practices)● Improves testability (because it is reusable)● Provides routing, history, jqLite, ...

Page 6: Nicolas Embleton, Advanced Angular JS

Bootstrapping

● Using angular-seeds○ git clone https://github.com/angular/angular-seed.git○ node scripts/web-server.js○ open http://localhost:8000/app/index.html

● Using yeoman (excellent workflow tool)○ (sudo) npm install -g yo○ (sudo) npm install -g generator-angular○ yo angular○ bower install angular-ui○ grunt server

Page 7: Nicolas Embleton, Advanced Angular JS

Now, the meat, the main features● Templating● Routing● 2-ways data-binding● Directives● Services● Inter-components Communication

Page 8: Nicolas Embleton, Advanced Angular JS

Templating● Models

○ 2-way binding○ Easy property mapping

● Built-in directives○ ngView

■ Where the routing happens○ ngRepeat

■ Iterator○ ngIf○ ngSwitch

Page 9: Nicolas Embleton, Advanced Angular JS

Templating - Conditional with ngIf<div ng-repeat="message in data.messages" ng-class="message.type"> <hr> <div ng-if="showFrom(message)"> <div>From: {{message.from.name}}</div> </div> <div ng-if="showCreatedBy(message)"> <div>Created by: {{message.createdBy.name}}</div> </div> <div ng-if="showTo(message)"> <div>To: {{message.to.name}}</div> </div> </div>

Page 10: Nicolas Embleton, Advanced Angular JS

Templating - Nested repeat<div ng-repeat="group in groups"><!-- 1st ng-repeat level --> <h2>{{ group.label }}</h2> <ul> <li ng-repeat="friend in group.friends"> <!-- 2nd ng-repeat level --> {{ friend.name }} </li> </ul><!-- END: Inner ngRepeat. --></div><!-- END: Outer ngRepeat. -->

Page 11: Nicolas Embleton, Advanced Angular JS

Templating - Example 2 - Switch<div ng-switch on="selection" > <div ng-switch-when="settings">Settings Div</div> <span ng-switch-when="home">Home Span</span> <span ng-switch-default>default</span></div>

Page 12: Nicolas Embleton, Advanced Angular JS

Example 2.2 - Switch with 2-way bind <div ng-controller="Ctrl">

<select ng-model="selection" ng-options="item for item in items" >

</select>

<tt>selection={{selection}} </tt>

<hr/>

<div class="animate-switch-container"

ng-switch on="selection">

<div ng-switch-when="settings">Settings Div</div>

<div ng-switch-when="home">Home Span</div>

<div ng-switch-default >default</div>

</div>

</div>

Page 13: Nicolas Embleton, Advanced Angular JS

Templating - Simple ngRepeat<li ng-repeat="item in items">

Item: {{ item }}

</li>

Page 14: Nicolas Embleton, Advanced Angular JS

Templating - Complex ngRepeat<header ng-repeat-start="item in items">

Header {{ item }}

</header>

<div class="body">

Body {{ item }}

</div>

<footer ng-repeat-end>

Footer {{ item }}

</footer>

Page 15: Nicolas Embleton, Advanced Angular JS

Compiling // compile the new DOM and link it to the current scope.

// NOTE: we only compile .childNodes so that

// we don't get into infinite loop compiling ourselves

$compile(element.contents())(scope);

Page 16: Nicolas Embleton, Advanced Angular JS

Templating - Routing

● Happens in ngView● Routing is a very powerful feature● Allows to update "pieces" of the page● Can stream files from disk to make it truly

isolated

Page 17: Nicolas Embleton, Advanced Angular JS

2-ways data-binding● Becoming more standard, thanks to frameworks like Ember.js or Angular.js● Linking 2 fields for synchronization purpose● Linking data to model● Automatically updating the template as data is changed

○ Arrays / Collections○ Inputs○ etc…

● Example

Page 18: Nicolas Embleton, Advanced Angular JS

<input type="text" ng-model="title" style="width: 90%"/><div ng-app="myapp"> <div ng-controller="mycontroller"> Title: {{ title }} <!-- 2-way data binding --> <hr> <div class="zippy" zippy-title="title"></div> </div></div>

2-ways data-binding, example

Page 19: Nicolas Embleton, Advanced Angular JS

● Angular.js killer feature● Great deal of re-usability● Just look for directives at ngmodules.org

Directives

Page 20: Nicolas Embleton, Advanced Angular JS

Restricting Directives

● "E": Element, <my-directive>● "A": Attribute, <div my-directive>● "C": Class, <div class="my-directive">● "M": Comment: <!-- directive: my-directive exp -->● Combine (e.g. "EA") for more flexibility

Page 21: Nicolas Embleton, Advanced Angular JS

Communicating between directives

● Many design patterns● The "backbonier"

○ the emitter and the receiver● A more connected example

○ the directive combinations and controller sharing

Page 22: Nicolas Embleton, Advanced Angular JS

Communicating between directivesapp.directive('directiveA', function($rootScope){ // $rootScope = App Scope

return function(scope, element, attrs) { // scope = Current scope (ctrl)

$rootScope.$on('someEvent', function(){

// From here we can react anytime there's an event "someEvent" triggered

});

};

});

Page 23: Nicolas Embleton, Advanced Angular JS

Communicating between directivesapp.directive('gdayx', function() { // Creating the directive

return {

restrict: 'E', // Restricted to "element"

controller: function($scope) { // Creating the controller of the directive

$scope.what = ""; // Local data

this.is = function(what) { // External accessor

$scope.what = what;

}

},

link: function($scope, $element){

$element.bind("click", function() { // Binding on click

alert("GDayX is "+$scope.what); // Getting content from the Controller.

});

}

}

});

Page 24: Nicolas Embleton, Advanced Angular JS

Communicating between directives// This directive will "send" data to the first directiveapp.directive('is', function() { // Creating the directive return { require: "gdayx", // Requiring the "gdayx" controller restrict: 'A', // Restricting to "attribute" link: function(scope, element, attrs, gdayxCtrl) { // gdayxCtrl from the "require: 'gdayx'" gdayxCtrl.is(attrs.is); // Passing value to the "gdayx" controller } }

});

Page 25: Nicolas Embleton, Advanced Angular JS

AngularJS - Why is it awesome?

● Mature, production-ready● Feature-rich● The design and what it allows● Strong support from giants like Google● A lot of solid companies are embracing it

○ Ebay Commerce Network○ DoubleClick (Google) - Marketing Manager &

Planner○ YouTube APP on PS3

Page 26: Nicolas Embleton, Advanced Angular JS

Best Practices

● Organize the code well (Captain Obvious!)● Organize modules by feature

angular.module('users', ['utilities']);angular.module('groups', ['utilities']);angular.module('mainApp', ['users', 'groups']);

● Use the reusability as much as possible● Use the testability as much as possible

○ TDD○ BDD?

Page 27: Nicolas Embleton, Advanced Angular JS

Testing, tooling● Yeoman

○ Super workflow tool and generator/scaffolder● Batarang

○ Chrome Debugger Extension, (A must have), link● Grunt

○ Task runner● Bower

○ Package Manager for JS Libraries● Protractor, Karma

○ Test Runner● Jasmine, Mocha

○ Test Frameworks

Page 28: Nicolas Embleton, Advanced Angular JS

And SEO?

● Google "Snapshot" famous technic○ _escaped_fragment_○ Turns this:

■ http://prerender.io/getting-started#html5-pushstate○ Into this:

■ http://prerender.io/getting-started?_escaped_fragment_=html5-pushstate

● prerender.io/ - Open Source project● brombone.com/ - Commercial project

Page 29: Nicolas Embleton, Advanced Angular JS

Enterprise project with Angular?

● YES● BUT

Page 30: Nicolas Embleton, Advanced Angular JS

Enterprise project with Angular?

● YES● BUT

○ Follow best practices (easier said than done)○ System Architecture is KEY to a solid system○ As "Agile" would advise, always try to go for simpler

but "well-thought" "team-friendly"designs.

Page 31: Nicolas Embleton, Advanced Angular JS

Enterprise project with Angular?

● An example Architecture

DB

Backend(legacy)

Legacy Front End

Experimental Front End

Experimental Front End 2

Backend (experimental)

A/B testing?

Server-side team realm Front-End team realm

Page 32: Nicolas Embleton, Advanced Angular JS

● Angular 1.0.x is mature● Angular 1.2+ will bring more awesomeness

○ Better and Fluid Animations (js/css3)○ More flexibility and functionalities

■ $interval: add a service wrapping setInterval■ Event directives: add ngCopy, ngCut, and ngPaste■ jQuery 1.10.x support

Final Words

Page 33: Nicolas Embleton, Advanced Angular JS

DEMO(if time permits :)

Page 34: Nicolas Embleton, Advanced Angular JS

Bootstrapped app

● Let's see a quick Angular App ● Bootstrapped from Yeoman

Page 35: Nicolas Embleton, Advanced Angular JS

Where you can find me:Author: Nicolas Embleton @: [email protected] made for “Google Developer Day GDayX 2013 Vietnam”

You can follow me at:● https://plus.google.com/+NicolasEmbleton● https://twitter.com/nicolasembleton

And the Javascript Ho Chi Minh City Meetup:● http://meetup.com/JavaScript-Ho-Chi-Minh-City/● https://www.facebook.com/JavaScriptHCMC● https://plus.google.com/communities/116105314977285194967