42
Sathish VJ Beginner’s Workshop

AngularJS Beginners Workshop

Embed Size (px)

DESCRIPTION

Material I prepared for a beginner's workshop on AngularJS. Feel free to change it for your own use. I would appreciate it if you attributed the original to me.

Citation preview

Page 1: AngularJS Beginners Workshop

Sathish VJ

Beginner’s Workshop

Page 2: AngularJS Beginners Workshop

History

• Origins within Google, invented by Misko Hevery

• Open-sourced 2009• 17,000 lines in 6 months => 1,500 in 3 weeks

Page 3: AngularJS Beginners Workshop

Killer Features

• Two-way binding• Declarative coding for UI• Teaching HTML new tricks• Separation of model & view, No DOM/View

manipulation, less code• Great for CRUD based single page apps• Awesome adoption and great community• Unit testable, E2E testable

Page 4: AngularJS Beginners Workshop

Main site, Download

• angularjs.org

Page 5: AngularJS Beginners Workshop

Ex 1: ng-app, expression binding

• Download https://github.com/sathishvj/AngularJSTutorials/archive/master.zip

• Extract locally• Go to 01-ngApp-binding• Open html in browser• Examine source code

Page 6: AngularJS Beginners Workshop

Ex 1: ng-app, expression binding

Page 7: AngularJS Beginners Workshop

Ex 1: ng-app, expression binding

• Include angular.js or angular.min.js• Add ng-app declarative where you want

Angular to manage app (typically <html> or <body>)

• Expression binding with {{ }}• ng-app can also be written as ngApp• Use camel case in JavaScript code (ngApp) and

use snake case in html (ng-app)

Page 8: AngularJS Beginners Workshop

How does it work

• Compile: traverse the DOM and collect all of the directives. The result is a linking function.

• Link: combine the directives with a scope and produce a live view.

Page 9: AngularJS Beginners Workshop

Ex 2: ngModel, binding variables

• 02-ngModel• ng-model binds variable data. It can be

declared on input, select, textarea, etc.

Page 10: AngularJS Beginners Workshop

Two-way data binding

Page 11: AngularJS Beginners Workshop

Other in-built declarations in Angular

ngApp ngBind ngBindHtml ngBindTemplate ngBlur ngChange ngChecked ngClass ngClassEven ngClassOdd ngClick ngCloak ngController ngCopy ngCsp ngCut ngDblclick ngDisabled ngFocus ngForm ngHide ngHref ngIf ngInclude ngInit ngKeydown ngKeypress ngKeyup ngList ngModel ngMousedown ngMouseenter ngMouseleave ngMousemove ngMouseover ngMouseup ngNonBindable ngOpen ngPaste ngPluralize ngReadonly ngRepeat ngSelected ngShow ngSrc ngSrcset ngStyle ngSubmit ngSwitch ngTransclude ngValue

Page 12: AngularJS Beginners Workshop

Ex 3: jquery comparison

• 03-jquerycomparison

Page 13: AngularJS Beginners Workshop

Differences from jQuery

• Declarative code, not imperative (explicitly stating, say with functions, what action needs to be performed)

• Automatic data binding• No direct DOM manipulation

Page 14: AngularJS Beginners Workshop

Ex 04: More expressions

Page 15: AngularJS Beginners Workshop

Expressions

• Expressions can be embedded anywhere• … even for class values• There are special filters that you can use

(currency)• Values are automatically bound

Page 16: AngularJS Beginners Workshop

MVC

Page 17: AngularJS Beginners Workshop

Ex 05: Scope and Controller

Page 18: AngularJS Beginners Workshop

Learnings

• Controller objects can separate data and functionality– There can be many controllers

• ‘$scope’ is injected by AngularJS• $scope.data in the controller is referred to as

simply data in html• Dot notation for object members works

(person.age)– In fact, this is suggested practice

Page 19: AngularJS Beginners Workshop

Scope, Controller, View

Page 20: AngularJS Beginners Workshop

How AngularJS does MVC

Page 21: AngularJS Beginners Workshop

Dependency Injection

Page 22: AngularJS Beginners Workshop

Dependency Injection

Page 23: AngularJS Beginners Workshop

Ex 07: Method on Scope

Page 24: AngularJS Beginners Workshop

Ex 08: ngClick

Page 25: AngularJS Beginners Workshop

Ex 09: ngRepeat

Page 26: AngularJS Beginners Workshop

Ex 09: ngRepeat

Page 27: AngularJS Beginners Workshop

Ex 10: Filters

Page 28: AngularJS Beginners Workshop

Other inbuilt filters

Page 29: AngularJS Beginners Workshop

Modules in AngularJS

• Third party code can be packaged as reusable modules.

• The modules can be loaded in any/parallel order (due to delayed nature of module execution).

• Some Angular modules are in separate files: Eg. ngRoute is in angularjs-route.min.js

Page 30: AngularJS Beginners Workshop

Ex 11: ngView and Routing

Page 31: AngularJS Beginners Workshop

ngView and Routing

• This part requires a web server– I’ve created a simple one at

https://github.com/sathishvj/gowebserver– Run suitable executable in index.html directory

• You can load different views contained in partial html files based on address bar location and apply specific controllers to it

Page 32: AngularJS Beginners Workshop

Ex 12: $http

Page 33: AngularJS Beginners Workshop

$http

• Make AJAX calls with $http• It returns deferred promises– More in later workshops

• You can handle ‘success’ and ‘error’

Page 34: AngularJS Beginners Workshop

Working with angular-seed

• https://github.com/angular/angular-seed• You need to have node.js installed to test

Page 35: AngularJS Beginners Workshop

Ex 13: Custom Filter

Page 36: AngularJS Beginners Workshop

Ex 14: Custom Directive

• -customdirective

Page 37: AngularJS Beginners Workshop

What we didn’t cover

• Unit testing, E2E testing• Custom directives beyond basics• $apply and digest cycles• $q and promises• Service, Factory, Provider

Page 38: AngularJS Beginners Workshop

Resources

• angularjs.org• Google Groups: AngularJS• Google+: AngularJS• ngmodules.org• Github: angular-ui.github.io• Google+: AngularJS India

Page 39: AngularJS Beginners Workshop

Project• Build an app to list tweets given a search string

(might require sign-up to Twitter API)

• Two pages required – main and an about page

Page 40: AngularJS Beginners Workshop

thank you

questions?

sathishvj

Page 41: AngularJS Beginners Workshop
Page 42: AngularJS Beginners Workshop