AngularJs Crash Course

download AngularJs Crash Course

If you can't read please download the document

  • date post

    16-Apr-2017
  • Category

    Technology

  • view

    8.459
  • download

    1

Embed Size (px)

Transcript of AngularJs Crash Course

Crash Course

Keith BloomfieldLead Developerdev9

Crash Course

Crash Course

Show of HandsFront-end developers?Back-end developers?Other?Using AngularJs at work/home?New to AngularJs?

Keith BloomfieldLead Developerdev9

OverviewData Binding, Expressions, Scopes,Ng DirectivesModules, Dependency InjectionControllers, Services, Factories, DirectivesUnit TestingEnd-to-End (e2e) TestingContinuous IntegrationReal World ExperiencesTool SupportResources for LearningQ&A

Crash Course

Agenda

Overview

AngularJs is a Spa framework

Manicure/Pedicure

Body/Facial Treatment

Massage

A lot has changed on the front-end in the lastfew years!

Overview

AngularJs is a Spa framework

Single-Page Application (dynamic views)

Recommends client-side Model-View-Controller (Mvc) pattern

Overview

AngularJs is a Spa framework

Single-Page Application (dynamic views)

Recommends client-side Model-View-Controller (Mvc) pattern

Don't know about each other.

Overview

AngularJs is a Spa framework

Single-Page Application (dynamic views)

Recommends client-side Model-View-Controller (Mvc) pattern

Overview

AngularJs is a Spa framework

Single-Page Application (dynamic views)

Recommends client-side Model-View-Controller (Mvc) pattern

Implications:RoutingHistoryCaching PackagingDeploymentAutomationData BindingObject ModelingTiming/DispatchTemplatingTestingStorage...

AngularJs

Overview

AngularJs is a Spa framework

Single-Page Application (dynamic views)

Recommends client-side Model-View-Controller (Mvc) pattern

What becomesof this?

Overview

AngularJs is a Spa framework

Single-Page Application (dynamic views)

Recommends client-side Model-View-Controller (Mvc) pattern

Model-View-Whatever (Mvw)

The View

Templates/Partials

foo.htmlHtml content swapped into/out of the single page

my-app.htmlmy-header.htmlmy-cart.htmlmy-news.htmla2.htmla2.htmlbn.htmlc1.htmla1.htmla3.htmlb3.htmlb2.htmlb1.htmlc2.htmld.htmlmy-mail.html

my-tools.html

Data Binding, Expressions and Scopes

Extending Html's vocabulary: Directives

Extend Html as markers on the Dom

Can be used as an attribute, element, class name, comment

Tell AngularJs' compiler ($compile) to attach specific behavior to that Dom element or even transform that element and its children.

echo

Name:
{{ echo }}

Angular (ng)Directives

Data BindingExpression

The compilation is a process of walking the DOM tree and matching DOM elements to directives. (links scope to template)

auto-bootstraps the AngularJs application/designates the root element.

binds input, select, textarea to aproperty on the scope using ng-model(NgModelController), and more.

Evaluated againstthe scope.

Data Binding, Expressions and Scopes

Extending Html's vocabulary: Directives

Extend Html as markers on the Dom

Can be used as an attribute, element, class name, comment

Tell AngularJs' compiler ($compile) to attach specific behavior to that Dom element or even transform that element and its children.

Data Binding, Expressions and Scopes

Extending Html's vocabulary: Directives

The only place where an AngularJs application touches the Dom is within directives.

This is good as artifacts that access the Dom are difficult to test.

More about directives later, but first...

Data Binding, Expressions and Scopes

$scope

Acts as the glue between the view and the controller

Refers to the application model

Arranged hierarchically to mimic the Dom

Can inherit from parent scope

Can watch expressions and propagate events

Data Binding, Expressions and Scopes

$scope

$rootScope: Defined at the top level of the application (where the ng-app directive is applied)

Lookup proceeds up the scope chain when a variable is not found in the local scope.

Relying on scope is risky because the Dom layout is often changed. - Isolating scope protects against changes to the Dom hierarchy.

Data Binding, Expressions and Scopes

$scope

$watch: Observe model mutations

$apply: Propagate model changes through the system into the view from outside the Angular realm (controllers, services, ng event handlers). Triggers $digest, which can be used directly in unit tests.

Also available: $watchCollection, $destroy, $eval, $evalAsync, $on, $emit, $broadcast

Dependency Injection

Reusable container for different features of your app

Module

Dependency Injection

Has two phases:config: setup providers and constants

run: after injector is created, kickstart the app

angular.module('myModule', ['moduleDependency1', 'moduleDependency2',....]) .config(function(injectable1, injectable2,...) { // provider-injector // Use as many of these as you want. // Can only inject Providers (not instances) }) .run(function(injectable1, injectable2,....) { // instance-injector // Use as many of these as you want. // Can only inject instances (not Providers) and constants });

Module

Dependency Injection

Module

Features can be bundled and injected in/as modules

Dependency Injection

$provide

- Object in module auto

auto- Implicit module which gets automatically added to each $injector

$injector- used to retrieve object instances as defined by provider, instantiate types, invoke methods, and load modules.

Dependency Injection

$provide

- Helper methods (also exposed on module):

provider(provider) - registers a service provider with the $injector

constant(obj) - registers a value/object that can be accessed by providers and services.

value(obj) - registers a value/object that can only be accessed by services, not providers.

factory(fn) - registers a service factory function, fn, that will be wrapped in a service provider object, whose $get property will contain the given factory function.

service(class) - registers a constructor function, class that will be wrapped in a service provider object, whose $get property will instantiate a new object using the given constructor function.

Worth investigating: $provide.decorator() // $provide onlyhttps://egghead.io/lessons/angularjs-provide-decorator

Dependency Injection

$provide

- Helper methods (also exposed on module):

provider(provider) - registers a service provider with the $injector

constant(obj) - registers a value/object that can be accessed by providers and services.

value(obj) - registers a value/object that can only be accessed by services, not providers.

factory(fn) - registers a service factory function, fn, that will be wrapped in a service provider object, whose $get property will contain the given factory function.

service(class) - registers a constructor function, class that will be wrapped in a service provider object, whose $get property will instantiate a new object using the given constructor function.

Where is the Controller?Added to the Dom using ng-controller

The ng-controller directive asks the injector to create an instance of the controller and its dependencies.

The controller itself never knows about the injector.

Loose coupling is maintained.

Dependency Injection

Dependencies are looked up by name for a provider that satisfies the argument.

app.controller(AController, function($scope, $http){$scope.userName = 'Walt'; $scope.foo = $http.get('http://foo.com');});

a.controller(AController, function(b, c){b.userName = 'Walt'; b.foo = c.get('http://foo.com');});

Minified:

Order doesn't matter$scope found in ng module - $rootScopeProvider - $rootScope.Scope$http service found ng module - $http

OH NO!

Dependency Injection

Longer version preserves dependency by treating it as an argument:

app.controller(AController, ['$scope', '$http', function($scope){$scope.userName = 'Walt'; $scope.foo = $http.get('http://foo.com');}]);

a.controller(AController, ['$scope', '$http', function(b, c){b.userName = 'Walt'; b.foo = c.get('http://foo.com');}]);

Minified:

Order matters

Passed in as argumentsto the function

Dependency Injection

Minification Pain Relief: ngminhttps://github.com/btford/ngmin

It turns thisangular.module('whatever').controller('MyCtrl', function ($scope, $http) { ... });into thisangular.module('whatever').controller('MyCtrl', ['$scope', '$http', function ($scope, $http) { ... }]);

Controllers

Used to augment the Angular scope

Attached to the Dom via the ng-controller directive

Set up the initial state of the $scope object

Add behavior to the $scope object

Our Pizzas:

{{ pizza.name }}...

app.controller('HomeController', function($scope){

$scope.pizzas = [ { name: "Meat Lover's", ingredients: ['Sausage','Pepperoni','Bacon','Olives'], price: 6 }, { name: 'Hawaiian', ingredients: ['Canadian bacon','Pineapple'], price: 5 }, ];});

!Controllers

Do not use a controller to:

Manipulate the Dom- Business logic only. Presentation logic affects testability and belongs in directives

Format input- Use angular form controls instead: https://docs.angularjs.org/guide/forms

Filter output- Use angular filters instead: