52
Deborah Kurata Consultant http://blogs.msmvps.com/deborahk [email protected]

Deborah Kurata - SDD Conferencesddconf.com/.../AngularJS_From_The_Ground_Up_Part_2.pdf · 2015-05-05 · AngularJS Line of Business Applications ... Part 2 New to Angular ... Consider

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Deborah Kurata - SDD Conferencesddconf.com/.../AngularJS_From_The_Ground_Up_Part_2.pdf · 2015-05-05 · AngularJS Line of Business Applications ... Part 2 New to Angular ... Consider

Deborah KurataConsultant

http://blogs.msmvps.com/deborahk

[email protected]

Page 2: Deborah Kurata - SDD Conferencesddconf.com/.../AngularJS_From_The_Ground_Up_Part_2.pdf · 2015-05-05 · AngularJS Line of Business Applications ... Part 2 New to Angular ... Consider

Deborah Kurata

Independent Consultant | Developer | Mentor

Web (Angular), .NET

Pluralsight Author

AngularJS Line of Business Applications

Angular Front to Back with Web API

Object-Oriented Programming Fundamentals in C#

Microsoft MVP

Page 3: Deborah Kurata - SDD Conferencesddconf.com/.../AngularJS_From_The_Ground_Up_Part_2.pdf · 2015-05-05 · AngularJS Line of Business Applications ... Part 2 New to Angular ... Consider

Session Materials & Sample Code

Page 4: Deborah Kurata - SDD Conferencesddconf.com/.../AngularJS_From_The_Ground_Up_Part_2.pdf · 2015-05-05 · AngularJS Line of Business Applications ... Part 2 New to Angular ... Consider

Why Angular?

Page 5: Deborah Kurata - SDD Conferencesddconf.com/.../AngularJS_From_The_Ground_Up_Part_2.pdf · 2015-05-05 · AngularJS Line of Business Applications ... Part 2 New to Angular ... Consider

Introduction to SPA

Rule-Based Navigation / Routing

Routing Frameworks (ngRoute/ngNewRouter)

JavaScript Promise

Accessing Data by Calling an HTTP service

Reusable Code: Building a Service

Overview - Part 2

Page 6: Deborah Kurata - SDD Conferencesddconf.com/.../AngularJS_From_The_Ground_Up_Part_2.pdf · 2015-05-05 · AngularJS Line of Business Applications ... Part 2 New to Angular ... Consider
Page 7: Deborah Kurata - SDD Conferencesddconf.com/.../AngularJS_From_The_Ground_Up_Part_2.pdf · 2015-05-05 · AngularJS Line of Business Applications ... Part 2 New to Angular ... Consider
Page 8: Deborah Kurata - SDD Conferencesddconf.com/.../AngularJS_From_The_Ground_Up_Part_2.pdf · 2015-05-05 · AngularJS Line of Business Applications ... Part 2 New to Angular ... Consider

New to Angular - no proficiency

Just starting out - limited proficiency

Doing it, not fully understanding it - working proficiency

Been there, done that, can help others - full proficiency

Rate Yourself on Routing

Page 9: Deborah Kurata - SDD Conferencesddconf.com/.../AngularJS_From_The_Ground_Up_Part_2.pdf · 2015-05-05 · AngularJS Line of Business Applications ... Part 2 New to Angular ... Consider

<head><link …/>

</head><body ng-app=

"movieHunter">

<script …/></body>

[menu code here]<ng-include=

"'movieListView.html'">

<div ng-controller="MovieListCtrl as vm">

...</div>

Page 10: Deborah Kurata - SDD Conferencesddconf.com/.../AngularJS_From_The_Ground_Up_Part_2.pdf · 2015-05-05 · AngularJS Line of Business Applications ... Part 2 New to Angular ... Consider

SPA

ingle age pplication

Only one page?

Only one full HTML page

All other pages are HTML Fragments called

templates

And are displayed within the one HTML page

Goals:

Enhance user experience

Reduce round tripping

Page 11: Deborah Kurata - SDD Conferencesddconf.com/.../AngularJS_From_The_Ground_Up_Part_2.pdf · 2015-05-05 · AngularJS Line of Business Applications ... Part 2 New to Angular ... Consider

Multiple Views

Page 12: Deborah Kurata - SDD Conferencesddconf.com/.../AngularJS_From_The_Ground_Up_Part_2.pdf · 2015-05-05 · AngularJS Line of Business Applications ... Part 2 New to Angular ... Consider

Routing

Page 13: Deborah Kurata - SDD Conferencesddconf.com/.../AngularJS_From_The_Ground_Up_Part_2.pdf · 2015-05-05 · AngularJS Line of Business Applications ... Part 2 New to Angular ... Consider

Site Map

Page 14: Deborah Kurata - SDD Conferencesddconf.com/.../AngularJS_From_The_Ground_Up_Part_2.pdf · 2015-05-05 · AngularJS Line of Business Applications ... Part 2 New to Angular ... Consider

Routes

Page 15: Deborah Kurata - SDD Conferencesddconf.com/.../AngularJS_From_The_Ground_Up_Part_2.pdf · 2015-05-05 · AngularJS Line of Business Applications ... Part 2 New to Angular ... Consider

Routing URL Fragments<mysite>/index.html#/<mysite>/index.html#/searchByTitle<mysite>/index.html#/showDetail/3

Page 16: Deborah Kurata - SDD Conferencesddconf.com/.../AngularJS_From_The_Ground_Up_Part_2.pdf · 2015-05-05 · AngularJS Line of Business Applications ... Part 2 New to Angular ... Consider

Routing Frameworks

Page 17: Deborah Kurata - SDD Conferencesddconf.com/.../AngularJS_From_The_Ground_Up_Part_2.pdf · 2015-05-05 · AngularJS Line of Business Applications ... Part 2 New to Angular ... Consider

ngRoute

Page 18: Deborah Kurata - SDD Conferencesddconf.com/.../AngularJS_From_The_Ground_Up_Part_2.pdf · 2015-05-05 · AngularJS Line of Business Applications ... Part 2 New to Angular ... Consider

UI Router

Page 19: Deborah Kurata - SDD Conferencesddconf.com/.../AngularJS_From_The_Ground_Up_Part_2.pdf · 2015-05-05 · AngularJS Line of Business Applications ... Part 2 New to Angular ... Consider

ngNewRouter

Page 20: Deborah Kurata - SDD Conferencesddconf.com/.../AngularJS_From_The_Ground_Up_Part_2.pdf · 2015-05-05 · AngularJS Line of Business Applications ... Part 2 New to Angular ... Consider

ngRoute

Page 21: Deborah Kurata - SDD Conferencesddconf.com/.../AngularJS_From_The_Ground_Up_Part_2.pdf · 2015-05-05 · AngularJS Line of Business Applications ... Part 2 New to Angular ... Consider

ngRoute: index.html

<div class="container"><div ng-view></div>

</div>

Page 22: Deborah Kurata - SDD Conferencesddconf.com/.../AngularJS_From_The_Ground_Up_Part_2.pdf · 2015-05-05 · AngularJS Line of Business Applications ... Part 2 New to Angular ... Consider

Routes

Page 23: Deborah Kurata - SDD Conferencesddconf.com/.../AngularJS_From_The_Ground_Up_Part_2.pdf · 2015-05-05 · AngularJS Line of Business Applications ... Part 2 New to Angular ... Consider

ngRoute: app.jsvar app = angular.module("movieHunter", ["ngRoute"]);

app.config(['$routeProvider',

function ($routeProvider) {

$routeProvider

.when("/", {

templateUrl: "app/welcomeView.html"

})

.when("/searchByTitle", {

templateUrl: "app/movies/movieListView.html",

controller: "MovieListCtrl as vm"

})

.when("/showDetail/:movieId", {

templateUrl: "app/movies/movieDetailView.html",

controller: "MovieDetailCtrl as vm"

})

.otherwise("/");

}]);

Page 24: Deborah Kurata - SDD Conferencesddconf.com/.../AngularJS_From_The_Ground_Up_Part_2.pdf · 2015-05-05 · AngularJS Line of Business Applications ... Part 2 New to Angular ... Consider

Navigation

Anchor tags

Link to the appropriate route

<a ng-href="#searchByTitle">

$location service

$location.path("/searchByTitle");

Page 25: Deborah Kurata - SDD Conferencesddconf.com/.../AngularJS_From_The_Ground_Up_Part_2.pdf · 2015-05-05 · AngularJS Line of Business Applications ... Part 2 New to Angular ... Consider

ngRoute: index.html

<div class="navbar-collapse collapse"><ul class="nav navbar-nav">

<li><a ng-href="#searchByTitle">Search by Title</a>

</li></ul></div>

Page 26: Deborah Kurata - SDD Conferencesddconf.com/.../AngularJS_From_The_Ground_Up_Part_2.pdf · 2015-05-05 · AngularJS Line of Business Applications ... Part 2 New to Angular ... Consider

ngRoute: movieDetailView.html

<div class="panel-footer"><a class="btn btn-default"

ng-href="#searchByTitle"style="width:80px"><i class="glyphicon glyphicon-chevron-left"></i>Back

</a></div>

Page 27: Deborah Kurata - SDD Conferencesddconf.com/.../AngularJS_From_The_Ground_Up_Part_2.pdf · 2015-05-05 · AngularJS Line of Business Applications ... Part 2 New to Angular ... Consider
Page 28: Deborah Kurata - SDD Conferencesddconf.com/.../AngularJS_From_The_Ground_Up_Part_2.pdf · 2015-05-05 · AngularJS Line of Business Applications ... Part 2 New to Angular ... Consider

ngNewRouter

Page 29: Deborah Kurata - SDD Conferencesddconf.com/.../AngularJS_From_The_Ground_Up_Part_2.pdf · 2015-05-05 · AngularJS Line of Business Applications ... Part 2 New to Angular ... Consider

ngNewRouter: index.html

<div class="container"><div ng-viewport></div>

</div>

Page 30: Deborah Kurata - SDD Conferencesddconf.com/.../AngularJS_From_The_Ground_Up_Part_2.pdf · 2015-05-05 · AngularJS Line of Business Applications ... Part 2 New to Angular ... Consider

ngNewRouter : app.js

var app = angular.module("movieHunter", ["ngNewRouter"]);

app.controller('AppController', ['$router', AppController]);

function AppController ($router) {$router.Config([

{ path: '/', component: 'welcome' },{ path: '/searchByTitle', component: 'movieList' },{ path: '/showDetail/:movieId', component: 'movieDetail' }

]);}

Page 31: Deborah Kurata - SDD Conferencesddconf.com/.../AngularJS_From_The_Ground_Up_Part_2.pdf · 2015-05-05 · AngularJS Line of Business Applications ... Part 2 New to Angular ... Consider

Defined by the

directory structure

Or configure with

$componentLoader

Routable Component in 1.4+

components/

welcome/welcome.html

movieList/movieList.htmlmovieList.js

movieDetail/movieDetail.htmlmovieDetail.js

Page 32: Deborah Kurata - SDD Conferencesddconf.com/.../AngularJS_From_The_Ground_Up_Part_2.pdf · 2015-05-05 · AngularJS Line of Business Applications ... Part 2 New to Angular ... Consider

ngNewRouter: movieDetailView.html

<div class="panel-footer"><a class="btn btn-default"

ng-link="movieList"style="width:80px"><i class="glyphicon glyphicon-chevron-left"></i>Back

</a></div>

Page 33: Deborah Kurata - SDD Conferencesddconf.com/.../AngularJS_From_The_Ground_Up_Part_2.pdf · 2015-05-05 · AngularJS Line of Business Applications ... Part 2 New to Angular ... Consider

Advanced Features

Multiple viewports

Sibling Components

Child Views

Tab-based UI

Lifecycle Hooks

canActivate | activate

canDeactivate | deactivate

Page 34: Deborah Kurata - SDD Conferencesddconf.com/.../AngularJS_From_The_Ground_Up_Part_2.pdf · 2015-05-05 · AngularJS Line of Business Applications ... Part 2 New to Angular ... Consider

Routing

SPA: Single Page Application

Use routing to display the views

Routing provides rule-based navigation

Define the routes using .config

Navigate to a route using <a>

Consider using the new 1.4 router

Page 35: Deborah Kurata - SDD Conferencesddconf.com/.../AngularJS_From_The_Ground_Up_Part_2.pdf · 2015-05-05 · AngularJS Line of Business Applications ... Part 2 New to Angular ... Consider
Page 36: Deborah Kurata - SDD Conferencesddconf.com/.../AngularJS_From_The_Ground_Up_Part_2.pdf · 2015-05-05 · AngularJS Line of Business Applications ... Part 2 New to Angular ... Consider

New to Angular - no proficiency

Just starting out - limited proficiency

Doing it, not fully understanding it - working proficiency

Been there, done that, can help others - full proficiency

Rate Yourself on Data Access

Page 37: Deborah Kurata - SDD Conferencesddconf.com/.../AngularJS_From_The_Ground_Up_Part_2.pdf · 2015-05-05 · AngularJS Line of Business Applications ... Part 2 New to Angular ... Consider

HTTP Service

What is an HTTP service?

Why call one from our Angular app?

Page 40: Deborah Kurata - SDD Conferencesddconf.com/.../AngularJS_From_The_Ground_Up_Part_2.pdf · 2015-05-05 · AngularJS Line of Business Applications ... Part 2 New to Angular ... Consider

Return immediately

When the operation is

complete

If the operation fails …

If the operation is successful …

Launch an asynchronous

operation

As the operation

progresses …

AsyncOperation

Up to 3 Callback Functions

JavaScript Promise

Page 41: Deborah Kurata - SDD Conferencesddconf.com/.../AngularJS_From_The_Ground_Up_Part_2.pdf · 2015-05-05 · AngularJS Line of Business Applications ... Part 2 New to Angular ... Consider

Calling HTTP Service

$http

$resource

$httpBackend

Page 42: Deborah Kurata - SDD Conferencesddconf.com/.../AngularJS_From_The_Ground_Up_Part_2.pdf · 2015-05-05 · AngularJS Line of Business Applications ... Part 2 New to Angular ... Consider

$http.get("/api/movies/")

.success(function(response) {

vm.movies = response.data;

});

Built into Angular Core

Facilitates communication with a back-end service

get, post, put, delete, …

Asynchronous

$http

Page 43: Deborah Kurata - SDD Conferencesddconf.com/.../AngularJS_From_The_Ground_Up_Part_2.pdf · 2015-05-05 · AngularJS Line of Business Applications ... Part 2 New to Angular ... Consider

REST

REpresentational State Transfer

Requests and responses involve the transfer

of resources

Resources are identified with a URL

○ /api/movies/

Requests utilize a standard set of HTTP verbs

○ GET | POST | PUT | DELETE

And much more!

Page 44: Deborah Kurata - SDD Conferencesddconf.com/.../AngularJS_From_The_Ground_Up_Part_2.pdf · 2015-05-05 · AngularJS Line of Business Applications ... Part 2 New to Angular ... Consider

function movieResource($resource) {

return $resource("/api/movies/:movieId")

}

Separate Angular component: angular-resource

ngResource

Abstraction on top of $http for calling RESTful

services

Requires less code

$resource

Page 45: Deborah Kurata - SDD Conferencesddconf.com/.../AngularJS_From_The_Ground_Up_Part_2.pdf · 2015-05-05 · AngularJS Line of Business Applications ... Part 2 New to Angular ... Consider

Reusable Data Access Service

Page 46: Deborah Kurata - SDD Conferencesddconf.com/.../AngularJS_From_The_Ground_Up_Part_2.pdf · 2015-05-05 · AngularJS Line of Business Applications ... Part 2 New to Angular ... Consider

Built-In $resource Methods

api/movies

api/movies/5

api/movies

Page 47: Deborah Kurata - SDD Conferencesddconf.com/.../AngularJS_From_The_Ground_Up_Part_2.pdf · 2015-05-05 · AngularJS Line of Business Applications ... Part 2 New to Angular ... Consider

$resource Methods

movieResource.query(function (data) {vm.movies = data;

});

Method Name URL Description

query /api/movies Retrieves all of the movies

get /api/movies/5 Retrieves the specified movie

save /api/movies Saves modifications to the movies

Page 48: Deborah Kurata - SDD Conferencesddconf.com/.../AngularJS_From_The_Ground_Up_Part_2.pdf · 2015-05-05 · AngularJS Line of Business Applications ... Part 2 New to Angular ... Consider
Page 49: Deborah Kurata - SDD Conferencesddconf.com/.../AngularJS_From_The_Ground_Up_Part_2.pdf · 2015-05-05 · AngularJS Line of Business Applications ... Part 2 New to Angular ... Consider
Page 50: Deborah Kurata - SDD Conferencesddconf.com/.../AngularJS_From_The_Ground_Up_Part_2.pdf · 2015-05-05 · AngularJS Line of Business Applications ... Part 2 New to Angular ... Consider

HTTP Service

Easily call an HTTP service using $resource

Steps:

Create a new common module

Add a movieResource service

Inject the service into a controller

Call the $resource methods

○ query, get, save

Page 51: Deborah Kurata - SDD Conferencesddconf.com/.../AngularJS_From_The_Ground_Up_Part_2.pdf · 2015-05-05 · AngularJS Line of Business Applications ... Part 2 New to Angular ... Consider

Take Away

Page 52: Deborah Kurata - SDD Conferencesddconf.com/.../AngularJS_From_The_Ground_Up_Part_2.pdf · 2015-05-05 · AngularJS Line of Business Applications ... Part 2 New to Angular ... Consider

Thank You!

@deborahkurata

[email protected]

http://blogs.msmvps.com/deborahk

https://github.com/DeborahK/SDD2015