AngularJS Enterprise Training Course

75
AngularJS Enterprise Training Course Troy Miles

description

In this AngularJS Enterprise Training Course participates will learn many of features in AngularJS which will give them a solid foundation of the framework. They will learn the fundamentals required to create testable, MVC-style single page applications. Students will become familiar with core concepts and will be shown advanced approaches using AngularJS. This class will include lecture and a series of instructor-led, hands-on labs. By the end of this class, participates will have the knowledge to leverage the power of AngularJS to build modern web applications easily and quickly!

Transcript of AngularJS Enterprise Training Course

Page 1: AngularJS Enterprise Training Course

AngularJS Enterprise Training CourseTroy Miles

Page 2: AngularJS Enterprise Training Course
Page 3: AngularJS Enterprise Training Course

https://github.com/Rockncoder/AngularJSEnterprise

Page 4: AngularJS Enterprise Training Course

Day One AgendaIntroduction to AngularJS

Lab - Two Way Data Binding

AngularJS Core Concepts

Lab - Shopping Cart

Morning Break

Filters

Lab - Using Filters

Page 5: AngularJS Enterprise Training Course

Day One Agenda (continue)

Lunch

Providers

Services

Factories

Client-side Routing

Afternoon Break

Lab - Routing

Page 6: AngularJS Enterprise Training Course

Day One Agenda (continue)

Directives

Lab - jQuery UI Widget

Day One Wrap-up

Page 7: AngularJS Enterprise Training Course

Beginning AngularJS

Page 8: AngularJS Enterprise Training Course

Introduction to AngularJS

Modern JavaScript Heavy Web Applications

Single Page Applications Explained

JavaScript Library vs. Framework

Contrasting AngularJS & jQuery

Other JavaScript MVC Frameworks

Page 9: AngularJS Enterprise Training Course

Modern JavaScript Heavy Web Applications

1996 - Microsoft introduces the iframe in IE

1999 -Microsoft introduces the XMLHTTP ActiveX control

2004 - GMail & Kayak, two heavily ajax’ed apps

2005 - Jesse James Garrett’s article coins the phrase AJAX

Page 10: AngularJS Enterprise Training Course

Text

Single Page Applications Explained

Page 11: AngularJS Enterprise Training Course

Library vs Framework

library framework

IoC no yes

default behavior no yes

extensibility yes yes

code bloat yes yes

Page 12: AngularJS Enterprise Training Course

Contrasting AngularJS & jQuery

AngularJS jQuery

framework yes no

extensible yes yes

IoC yes no

default behavior yes no

minified size 106 kb 84 kb*

Page 13: AngularJS Enterprise Training Course

Lab - jQuery Data Binding

Page 14: AngularJS Enterprise Training Course

jQuery Example<!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8"> <title>Binding jQuery Example</title> <link rel="stylesheet" href="../css/bootstrap.css"/> <script type="text/javascript" src="../libs/jquery-1.11.1.min.js"></script></head><body> <div class="container" > <h1>Greet-o-matic</h1> <input type="text" id="firstName"/> <hr/> <p>Hello <span id="showName"></span>,<br/>Have a nice day!</p> </div> <script type="text/javascript"> $( document ).ready( function(){ $('#firstName').on("keyup", function(evt){ $('#showName').text(this.value); }); }); </script></body></html>

Page 15: AngularJS Enterprise Training Course

AngularJS Example<!DOCTYPE html><html ng-app><head lang="en"> <meta charset="UTF-8"> <title>Binding AngularJS Example</title> <link rel="stylesheet" href="../css/bootstrap.css"/> <script type="text/javascript" src="../libs/angular.js"></script></head><body> <div class="container" > <h1>Greet-o-matic</h1> <input type="text" ng-model="userName"/> <hr/> <p>Hello {{userName}},<br/>Have a nice day!</p> </div></body></html>

Page 16: AngularJS Enterprise Training Course

Two-way Data Binding<!DOCTYPE html><html ng-app><head lang="en"> <meta charset="UTF-8"> <title>Binding AngularJS Example</title> <link rel="stylesheet" href="../css/bootstrap.css"/> <script type="text/javascript" src="../libs/angular.js"></script></head><body><div class="container" > <h1>Greet-o-matic</h1> <div class="col-lg-6"> <input type="text" ng-model="userName" placeholder="Enter name here"/> </div> <div class="col-lg-6"> <input type="text" ng-model="userName" placeholder="or over here"/> </div> <hr/> <p>Hello <span>{{userName}}</span>,<br/>Have a nice day!</p></div></body></html>

Page 17: AngularJS Enterprise Training Course

Other JavaScript MVC Frameworks

Backbone.js

Knockout

EmberJS

Other frameworks & ToDoMVC

Page 18: AngularJS Enterprise Training Course

Backbone.jsCreated by Jeremy Ashkenas in 2010

19 kb production version (minimized, not gzipped)

One dependency - Underscore.js, optional jQuery

Three core concepts: models, collections, & views

Uses lots of custom events

Page 19: AngularJS Enterprise Training Course

KnockoutCreated by Steve Sanderson in 2010

47 kb production version (minimized, not gzipped)

Uses MVVM pattern

Two way data-binding

No dependencies

Supports all mainstream browsers

Page 20: AngularJS Enterprise Training Course

EmberCreated by Yehuda Katz and Tom Dale in 2011

Convention over configuration

Ember Data, a separate package, handles RESTful data

Handlebars.js, a separate package, handles templates

337 kb production version (minimized, not gzipped)

Page 21: AngularJS Enterprise Training Course

Other frameworksDojo

YUI

Agility.js

Knockback.js

CanJS

Maria

Polymer

React

cujoJS

Montage

Sammy.js

Stapes

Batman.js

http://todomvc.com/

Page 22: AngularJS Enterprise Training Course

AngularJSCreated by Miško Hevery and Adam Abrons in 2009

JavaScript MVC

106 kb production version (minimized, not gzipped)

Declarative programming for UI

Imperative programming for business logic

Page 23: AngularJS Enterprise Training Course

AngularJS Key Features

MVC

HTML Templates

Dependency Injection

Deep Linking

Directives

Page 24: AngularJS Enterprise Training Course

Angular Core Concepts

Understanding AngularJS’ MVC Architecture

Module

Dependency Injection

Data Binding

Page 25: AngularJS Enterprise Training Course

Understanding AngularJS’ MVC architecture

Uses MVC or MVVM or MV* depends on who you ask

The goal is clear separation of concerns

The model is only concerned with data

The view presents the data to the user

The controller applies the business logic

Page 26: AngularJS Enterprise Training Course

Module

A container for different parts of your app

Most apps will have one module

Most 3rd party libraries will come with their own

Can create extra modules for more complex apps via JavaScript

Page 27: AngularJS Enterprise Training Course

Dependency InjectionA software design pattern that implements inversion of control and allows a program design to follow the dependency inversion principle

Allows a dependency to be passed to an object

Allows code to clearly state dependencies

Leads to code which is easier to debug and test

Makes it possible to minimize apps without breaking them

Page 28: AngularJS Enterprise Training Course

Dependency InjectionThe DI pattern in AngularJS looks funky due to JavaScript’s shortcomings

The pattern is name of module, dot, name of provider, name of object, an array with the list of dependencies in strings and a function as the last item in the array

tempApp.controller('CurrentCtrl', ['$scope', function ($scope) { $scope.temp = 17; }]);

Page 29: AngularJS Enterprise Training Course

Data Binding

In Angular, binding is built into the framework

Replaces text content of HTML element with the value of the expression

{{ expression }}

<ANY ng-bind=“expression”>…</ANY>

<ANY class=“ng-bind: expression;”>…</ANY>

Page 30: AngularJS Enterprise Training Course

Lab - Two Way Data Binding

Page 31: AngularJS Enterprise Training Course

Intermediate AngularJS

Page 32: AngularJS Enterprise Training Course

Filters

Understanding Filters

A tour of built-in filters

ngSanitize

Building custom Filters

Lab

Page 33: AngularJS Enterprise Training Course

Understanding FiltersUsed to format data displayed to user

Strictly front-end, doesn’t change model data

Accessible using declarative or imperative syntax

{{ expression [| filter_name[:parameter_value] ... ] }}

$scope.originalText = 'hello';$scope.filteredText = $filter('uppercase')($scope.originalText);

Page 34: AngularJS Enterprise Training Course

A tour of built-in filterscurrency

date

json

lowercase

uppercase

number

filter

limitTo

orderBy

Page 35: AngularJS Enterprise Training Course

ngSantize

Provides functionality to sanitize HTML

Parses the HTML into tokens

Safe tokens converted into properly escaped HTML strings

May be a bit overzealous

Page 36: AngularJS Enterprise Training Course

Building custom filterstempApp.filter('minimum', [function () { return function (arrTemp, minimum) { var filteredArray = []; var min = minimum ? minimum : 15; angular.forEach(arrTemp, function (value, key) { if (value.temp >= min) filteredArray.push(value); }); return filteredArray; }; }]); !

Page 37: AngularJS Enterprise Training Course

Lab - Using Filters

Page 38: AngularJS Enterprise Training Course

Providers

What are providers?

Types of providers

Services

Factories

Providers

Page 39: AngularJS Enterprise Training Course

What are providers?

Objects that are instantiated and wired together automatically by the injector service

The injector creates two kinds of objects:

services - defined by the developer

specialized objects - Angular framework pieces, controllers, directives, filters, or animations

Page 40: AngularJS Enterprise Training Course

Types of providersConstants

Value

Decorator

Provider

Service

Factory

Page 41: AngularJS Enterprise Training Course

Services

Substitutable objects that are wired together using DI

Used to organize and share code across app

Only instantiated when an app component depends on it

Singletons

Built-in services always start with “$”

Page 42: AngularJS Enterprise Training Course

Factories

Introduction to shared components

Dependency injection deep dive

Building custom factories & services

Persisting data to a Web API service

Page 43: AngularJS Enterprise Training Course

Client-side Routing

Server-side routing vs client-side routing

ngRoute module

HTML URLs vs hash based URLs

Page 44: AngularJS Enterprise Training Course

Server-side Routing vs Client-side Routing

Traditionally changing the URL triggers server side request

Angular watches $location.url() and tries to map it to a route definition

Page 45: AngularJS Enterprise Training Course

ngRoute

Provides routing and deep linking services and directives

Must include angular-route.js in HTML, after angular.js

Must mark ngRoute as a dependent module

Page 46: AngularJS Enterprise Training Course

HTML URLs vs hash based URLs

By default Angular uses hash “#” based URLs

It can use HTML5 push state, with fallback

$locationProvider.html5Mode(true);

Page 47: AngularJS Enterprise Training Course

Lab - Routing

Page 48: AngularJS Enterprise Training Course

Directives

Introduction to Directives

jQuery integration

Using a jQuery UI Widget

Page 49: AngularJS Enterprise Training Course

Directives

Markers on a DOM element that attach a behavior to it

Can be an attribute, element name, comment, or CSS

The HTML compiler traverses the DOM at bootstrap and matches directives to DOM elements

Page 50: AngularJS Enterprise Training Course

Directives Names<div timePicker></div>

<div time-picker></div>

<div time:picker></div>

<div time_picker></div>

<div x-time-picker></div>

<div data-time-picker></div>

Page 51: AngularJS Enterprise Training Course

Directive Location

Tag name: <timePicker></timePicker>

Attribute: <div timePicker></div>

Class: <div class=“time-picker;”></div>

Comment: <!— directive:time-picker —>

Page 52: AngularJS Enterprise Training Course

Built-in Directives

ng-app

ng-bind

ng-controller

ng-href

ng-readonly

ng-repeat

ng-src

ng-submit

ng-transclude

ng-view

Page 53: AngularJS Enterprise Training Course

jQuery Integration

AngularJS includes a mini version of jQuery called jqLite

It is perfectly compatible with the full version of jQuery

jQuery must be loaded before Angular or it won’t see it

Page 54: AngularJS Enterprise Training Course

Using a jQuery Widgetapp.directive('timePicker', function () { var today = new Date(new Date().toDateString()); return { require: '?ngModel', link: function ($scope, $element, $attrs, ngModel) { var initialized = false; ngModel = ngModel || { "$setViewValue": angular.noop }; // where is the missing time value? setTimeout(function () { initialized = $element.timepicker() .on('changeTime', function (ev, ui) { var sec = $element.timepicker('getSecondsFromMidnight'); ngModel.$setViewValue(sec * 1000); console.log("sec = " + sec); }); }); ngModel.$render = function (val) { if (!initialized) { //If $render gets called before our timepicker plugin is ready, just return return; } $element.timepicker('setTime', new Date(today.getTime() + val)); } } }});

Page 55: AngularJS Enterprise Training Course

Lab - jQuery UI Widget

Page 56: AngularJS Enterprise Training Course

Day Two: Advanced AngularJS

Page 57: AngularJS Enterprise Training Course

Day Two AgendaDay One Recap

Lab - Simple Calculator

Project Organization

Best Practices

Digest Cycle

Promises

Lab - Promises

Page 58: AngularJS Enterprise Training Course

Day Two Agenda (continue)

Morning Break

Directives Deep Dive

Lab - Custom Directives

Lunch

Unit Test and Test Driven Development

Lab - TDD & AngularJS

Angular UI

Page 59: AngularJS Enterprise Training Course

Day Two Agenda (continue)

Afternoon Break

ASP.NET MVC & Web API with Angular

Lab Bootstrap & AngularJS

Tips & Traps

Summary

Page 60: AngularJS Enterprise Training Course

Day One Recap

AngularJS Core Concepts

Filters

Providers

Services

Factories

Page 61: AngularJS Enterprise Training Course

Project Organization

Page 62: AngularJS Enterprise Training Course

Best PracticesModel dot notation

$scope best practices

jQuery placement via directives, not controllers

Separation of concerns

Injecting models into event handlers vs referencing $scope

Fast idempotent $watch expressions

Page 63: AngularJS Enterprise Training Course

The Digest Cycle

$watch explained

Digest cycle deep dive

External events

Understanding $apply

Page 64: AngularJS Enterprise Training Course

Promises

The promise pattern

Promises in AngularJS with $q

$q and sevices

$q and 3rd party callbacks

Page 65: AngularJS Enterprise Training Course

Directives Deep Dive

Revisiting directives

Isolated scope

Link & compile functions

Templating

jQuery integration

Page 66: AngularJS Enterprise Training Course

Directives Deep Dive

Examining different ways to write a directive

Wrapping a jQuery widget in a directive

Shadow DOM and the future of directives

Page 67: AngularJS Enterprise Training Course

Lab: Building Custom Directives

Page 68: AngularJS Enterprise Training Course

Unit Testing and TDD

Introduction to Test Driven Development

Jasmine

Karma Test Runner

Mocking

Dependency Injection and Testing

Page 69: AngularJS Enterprise Training Course

Lab: Creating an Angular App using TDD

Page 70: AngularJS Enterprise Training Course

Angular UI

Quick Bootstrap overview

Integrating AngularJS and Bootstrap

Page 71: AngularJS Enterprise Training Course

AngularJS and ASP.NET

Routing differences

Single page apps vs traditional apps

Authentication & authorization

Angular & razor views

Page 72: AngularJS Enterprise Training Course

Lab: Bootstrap and AngularJS

Page 73: AngularJS Enterprise Training Course

Lab: Angular and Web API

Page 74: AngularJS Enterprise Training Course

Summary

Page 75: AngularJS Enterprise Training Course

Resources

Slides & Notes

Links

https://bitly.com/bundles/rockncoder/6