angular js level 01-05

Embed Size (px)

Citation preview

  • 7/25/2019 angular js level 01-05

    1/164

  • 7/25/2019 angular js level 01-05

    2/164

    Shaping Up with Angular

    Level 1: Getting Started

  • 7/25/2019 angular js level 01-05

    3/164

    What you need to know

    Must know Not so important

    Nice to know

    Automated Testing

    BDD - Behavior Driven Development

    TDD - Test Driven Development

    etc

    HTML & CSS

    JavaScript jQueryRuby on Rails

    Python, PHP, etc

    Databases

  • 7/25/2019 angular js level 01-05

    4/164

    Why Angular?

    If youre using JavaScript to create a dynamic website,

    Angular is a good choice.

    Angular helps you organize your JavaScript

    Angular helps create responsive (as in fast) websites.

    Angular plays well with jQuery

    Angular is easy to test

  • 7/25/2019 angular js level 01-05

    5/164

    Traditional Page-Refresh

    Screencast: Request-Response Application

    NOTE: Time between user

    interaction and response.

  • 7/25/2019 angular js level 01-05

    6/164

    Web Server

    HTML Java

    Response with Webpage & Assets

    Web Browser

    URL Request to server

    Response with Webpage & Assets

    User clicks on link, new Request HTML Java

    Browser load

    entire webp

    Browser load

    entire webp

  • 7/25/2019 angular js level 01-05

    7/164

    Screencast: Request-Response ApplicationNOTE: Note how responsive the page is

    A responsive website using Angular

  • 7/25/2019 angular js level 01-05

    8/164

    Web Server

    Response with Webpage & Assets

    Web Browser

    URL Request toserver

    Response with JSON Data

    User clicks on link,new Request

    HTML JavaS

    DATA

    Browser loa

    entire webp

    Data is loade

    existing p

  • 7/25/2019 angular js level 01-05

    9/164

    Modern API-Driven Application

    HTML BrowsApp

    Data

    Source

    MobileApp

    Developers

    Server

    API

  • 7/25/2019 angular js level 01-05

    10/164

    A client-side JavaScript Framework for adding interactivity to

    index.html

    How do we tell our HTMLwhen to trigger ourJavaScript?

    What is Angular JS?

  • 7/25/2019 angular js level 01-05

    11/164

  • 7/25/2019 angular js level 01-05

    12/164

    The Flatlanders Store!

    Screencast: Flatlanders Store

    Fully Functional

  • 7/25/2019 angular js level 01-05

    13/164

    Download AngularJS http://angularjs.org/

    Well needangular.min.js

    Downloading the libraries

    Download Twitter Bootstrap http://getbootstrap.com/

    Well needbootstrap.min.css

  • 7/25/2019 angular js level 01-05

    14/164

  • 7/25/2019 angular js level 01-05

    15/164

    Modules

    Where we write pieces of our Angular application.

    Makes our code more maintainable, testable, and reada Where we define dependencies for our app.

    Modules can use other Modules...

  • 7/25/2019 angular js level 01-05

    16/164

    Creating Our First Module

    DependenciesOther libraries we might need.

    We have none... for now

    Application

    NameAngularJS

    varapp =angular.module('store', []);

  • 7/25/2019 angular js level 01-05

    17/164

    Including Our Module

    varapp =angular.module('store', []);app.js

  • 7/25/2019 angular js level 01-05

    18/164

    Including Our Module

    varapp =angular.module('store', []);app.js

  • 7/25/2019 angular js level 01-05

    19/164

    Allow you to insert dynamic values into your HTML.

    Expressions

    Numerical Operations

    evaluates to

    String Operations

    evaluates to

    + More Operations:http://docs.angularjs.org/guide/expression

    I am {{4 + 6}}

    {{"hello" + " you"}}

    I am 10

    hello you

  • 7/25/2019 angular js level 01-05

    20/164

    Including Our Module

    varapp =angular.module('store', []);app.js

  • 7/25/2019 angular js level 01-05

    21/164

    Challenges

  • 7/25/2019 angular js level 01-05

    22/164

    Working With Data

    ...just a simple

    object we want toprint to the page.

    vargem ={

    name: 'Dodecahedron',

    price: 2.95,

    description: '. . .',

    }.

  • 7/25/2019 angular js level 01-05

    23/164

    Controllers

    app.js

    Notice that controller is attached to (inside) our app.

    Controllers are where we define our apps behavior by de

    functions and values.

    Wrapping your Javascriptin a closure is a good habit!

    vargem ={

    name: 'Dode

    price: 2.95

    description

    }.

    })();

    (function(){

    varapp =angular.module('store', []);

    app.controller('StoreController', function(){

    });

  • 7/25/2019 angular js level 01-05

    24/164

    Storing Data Inside the Controller

    app.js

    Now hoprint odata in

    webpthis.product =gem;

    vargem ={

    name: 'Dodecahedron', price: 2.95,

    description: '. . .',

    }.

    })();

    (function(){

    varapp =angular.module('store', []);

    app.controller('StoreController', function(){

    });

  • 7/25/2019 angular js level 01-05

    25/164

  • 7/25/2019 angular js level 01-05

    26/164

    Product Name

    $Product Price

    Product Description

    (function(){

    varapp =angular.module('store', []);

    app.controller('StoreController', function(){

    this.product =gem;

    });

    . . .

    })();

    app.js

    index.html

    Attaching the Controller

  • 7/25/2019 angular js level 01-05

    27/164

    Product Name

    $Product Price

    Product Description

    Attaching the Controller

    (function(){

    varapp =angular.module('store', []);

    app.controller('StoreController', function(){

    this.product =gem;

    });

    . . .

    })();

    app.js

    index.html

    Controller name AliasDirective

  • 7/25/2019 angular js level 01-05

    28/164

    index.html

    Displaying Our First Product

    (function(){

    varapp =angular.module('store', []);

    app.controller('StoreController', function(){

    this.product =gem;

    });

    . . .

    })();

    app.js

    {{store.product.name}}

    ${{store.product.price}}

    {{store.product.description}}

  • 7/25/2019 angular js level 01-05

    29/164

    Understanding Scope

    Would never print avalue!

    }{{store.product.name}}

    {{store.product.name}}

    ${{store.product.price}}

    {{store.product.description}}

  • 7/25/2019 angular js level 01-05

    30/164

    Challenges

    Addi A B

  • 7/25/2019 angular js level 01-05

    31/164

    {{store.product.name}}

    ${{store.product.price}}

    {{store.product.description}}

    Adding A Button

    index.html

    vargem ={

    name: 'Dodecahedron',

    price: 2.95,

    description: '. . .',

    }.

    Addi A B

  • 7/25/2019 angular js level 01-05

    32/164

    vargem ={

    name: 'Dodecahedron',

    price: 2.95,

    description: '. . .',

    }.

    canPurchase: false

    Adding A Button

    index.html

    Add to Cart

    N Sh Di ti

  • 7/25/2019 angular js level 01-05

    33/164

    vargem ={

    name: 'Dodecahedron',

    price: 2.95,

    description: '. . .',

    }.

    canPurchase: false

    NgShow Directive

    index

    Add to Cart

  • 7/25/2019 angular js level 01-05

    34/164

    N Hid Di ti

  • 7/25/2019 angular js level 01-05

    35/164

    index

  • 7/25/2019 angular js level 01-05

    36/164

    index

  • 7/25/2019 angular js level 01-05

    37/164

    index

  • 7/25/2019 angular js level 01-05

    38/164

    Multiple Products

    vargem =

    name: "Dodecahedron",

    price: 2.95,

    description: ". . .",

    canPurchase: true,

    },

    {

    app.controller('StoreController', function(){

    this.product =gem

    });

    ;

    Multiple Products

  • 7/25/2019 angular js level 01-05

    39/164

    Multiple Products

    vargems =[

    name: "Dodecahedron",

    price: 2.95,

    description: ". . .",

    canPurchase: true,

    },

    { name: "Pentagonal Gem",

    price: 5.95,

    description: ". . .",

    canPurchase: false,

    }

    ];

    {Now we have an array...

    How might we display all thesproducts in our template?

    MaybeaDirective?

    app.controller('StoreController', function(){

    this.products =gems

    });

    ;

    So we have multiple products...

    Working with An Array

  • 7/25/2019 angular js level 01-05

    40/164

    Working with An Array

    {{store.products[0].name}}

    ${{store.products[0].price}}

    {{store.products[0].description}}

    Add to Cart

    . . .

    Working with An Array

  • 7/25/2019 angular js level 01-05

    41/164

    Working with An Array

    . . .

    Displaying the first is easy enough...

    {{store.products[0].name}}

    ${{store.products[0].price}}

    {{store.products[0].description}}

    Working with An Array

  • 7/25/2019 angular js level 01-05

    42/164

    Working with An Array

    That works...

    Working with An Array

  • 7/25/2019 angular js level 01-05

    43/164

    Working with An Array

    What We Have Learned So Far

  • 7/25/2019 angular js level 01-05

    44/164

    What We Have Learned So Far

    Directives HTML annotations that trigger Javascript

    Modules Where our application components live

    Controllers Where we add application behavior

    Expressions How values get displayed within the pa

  • 7/25/2019 angular js level 01-05

    45/164

    Challenges

  • 7/25/2019 angular js level 01-05

    46/164

  • 7/25/2019 angular js level 01-05

    47/164

  • 7/25/2019 angular js level 01-05

    48/164

    Shaping Up with Angular.JS

    Level 2: Filters, Directives, and Cleaner Code

    Directives We Know & Love

  • 7/25/2019 angular js level 01-05

    49/164

    ng-app attach the Application Module to the page

    ect es e o & o e

    ng-controller attach a Controller function to the page

    ng-show / ng-hide display a section based on an Expressio

    ng-repeat repeat a section for each item in an Array

    Hello, {{name}}!

    {{product.name}}

    Our Current Code

  • 7/25/2019 angular js level 01-05

    50/164

  • 7/25/2019 angular js level 01-05

    51/164

    Pipe - send the output intoFormat this

    Notice it gives the dollar sign Specifies number of decimals

    {{product.price| currency }

  • 7/25/2019 angular js level 01-05

    52/164

    {{ data | filter:options }}

    g

    *Our Recipe

    {{'1388123412323' | date:'MM/dd/yyyy @ h:mma'}}

    date

    12/27/20

    {{'octagon gem' | uppercase}}

    uppercase & lowercase

    OCTAGON

    {{'My Description' | limitTo:8}}

    limitTo

    My Descr

  • 7/25/2019 angular js level 01-05

    53/164

    vargems = [

    {name: 'Dodecahedron Gem',

    price: 2.95, description: '. . .',

    images: [

    {

    full: 'dodecahedron-01-full.jpg',

    thumb: 'dodecahedron-01-thumb.jpg'

    },

    {

    full: "dodecahedron-02-full.jpg",

    . . .

    g g y y

    Our New ArrayImage

    To display the first image in a product: {{product.images[0].f

    Using ng-src for Images

  • 7/25/2019 angular js level 01-05

    54/164

  • 7/25/2019 angular js level 01-05

    55/164

    g

  • 7/25/2019 angular js level 01-05

    56/164

    Challenges

    More With Directives

  • 7/25/2019 angular js level 01-05

    57/164

    How can I make my amore interactive?

    A Simple Set of Tabs

  • 7/25/2019 angular js level 01-05

    58/164

  • 7/25/2019 angular js level 01-05

    59/164

    Assigning a value to tab.For now just print this value to the screen.

  • 7/25/2019 angular js level 01-05

    60/164

    g

    Whoa, its dynamic and stuff...

  • 7/25/2019 angular js level 01-05

    61/164

    When ng-clickchanges the value of tab

    the {{tab}}expression automatically gets updated!

    Expressions define a 2-way Data Binding ...

    this means Expressions are re-evaluated when a property cha

    Lets add the tab content panels

  • 7/25/2019 angular js level 01-05

    62/164

    tabs are up here.... . .

    Lets add the tab content panels

  • 7/25/2019 angular js level 01-05

    63/164

    Now when a tab is selected it will show the appropriate pa

    >

    >

    Description

    {{product.description}}

    Specifications

    None yet

    Reviews

    None yet

    ng-show="tab === 1"

    ng-show="tab === 2"

    ng-show="tab === 3"

  • 7/25/2019 angular js level 01-05

    64/164

    initial va

    Setting the Initial Value

  • 7/25/2019 angular js level 01-05

    65/164

    ng-initallows us to evaluate an expression in the curre

    Description

    Specifications

    Reviews

    . . .

    Now with the Initial Tab

  • 7/25/2019 angular js level 01-05

    66/164

    Setting the Active Class

  • 7/25/2019 angular js level 01-05

    67/164

    We need to set the classto activeif current tabis se

    ng-init="tab = 1">

  • 7/25/2019 angular js level 01-05

    68/164

    Name of the class to set.

    Expression to evIf true, set classotherwise nothing

    ng-init="tab = 1">

  • 7/25/2019 angular js level 01-05

    69/164

    Feels dirty, doesnt it?

  • 7/25/2019 angular js level 01-05

    70/164

    All our applications logic is inside our HTML.

    How might logic into a

    tab === 1

    ng-class="{ active: }">tab === 2

    ng-class="{ active: }">tab === 3

    ng-init="tab = 1">

    Creating our Panel Controller

  • 7/25/2019 angular js level 01-05

    71/164

    ng-init="tab = 1"

    >

    Moving our tab initializer

  • 7/25/2019 angular js level 01-05

    72/164

    ng-controller="PanelController as panel">

    Creating our selectTab function

  • 7/25/2019 angular js level 01-05

    73/164

    }">

    }">

    tab === 1

    tab === 2

    tab === 3

    this.tab =setTab;

    };

    function(setTabthis.selectTab=

    Creating our isSelected function

  • 7/25/2019 angular js level 01-05

    74/164

    }">

    }">

  • 7/25/2019 angular js level 01-05

    75/164

  • 7/25/2019 angular js level 01-05

    76/164

    Challenges

  • 7/25/2019 angular js level 01-05

    77/164

  • 7/25/2019 angular js level 01-05

    78/164

  • 7/25/2019 angular js level 01-05

    79/164

    Shaping Up with Angular

    Level 3: Forms, Models, and Validations

    More Directives: Forms & Models

  • 7/25/2019 angular js level 01-05

    80/164

    How can I let my users ad

    Adding reviews to our products

  • 7/25/2019 angular js level 01-05

    81/164

    app.controller("StoreController",function(){

    this.products =[

    {

    name:'Awesome Multi-touch Keyboard',

    price:250.00, description:"...",

    images: [...],

    reviews: [

    {

    stars:5,

    body:"I love this product!",

    author:"[email protected]"

    }, {

    stars:1,

    body:"This product sucks",

    author:"[email protected]"

    }

    . . .

    Looping Over Reviews in our Tab

  • 7/25/2019 angular js level 01-05

    82/164

  • 7/25/2019 angular js level 01-05

    83/164

    Nothing new here, but how do we start to implement forms

    Writing out our Review Form

  • 7/25/2019 angular js level 01-05

    84/164

    Reviews

    ...

  • 7/25/2019 angular js level 01-05

    85/164

    Stars: {{review.stars}}

    {{review.body}}by: {{review.author}}

    How do we bind object to the form

    1 star

    2 stars

    . . .

    by:

    Introducing ng-model&,

  • 7/25/2019 angular js level 01-05

    86/164

    ng-model="review.stars"

    ng-model="review.body"

    ng-model="review.author"

    1 star

    2 stars

    . . .

    by:

    Stars: {{review.stars}}

    {{review.body}}by: {{review.author}}

    ng-model bin

    form elemento the proper

    Live Preview In Action

  • 7/25/2019 angular js level 01-05

    87/164

    But how do we actually add the new review?

    Two More Binding Examples

    With a Checkbox

  • 7/25/2019 angular js level 01-05

    88/164

    I agree to t

    With a Checkbox

    With Radio Buttons

    Sets value to true or false

    Sets the proper value based on which is selected

    What color would you like?

  • 7/25/2019 angular js level 01-05

    89/164

    Challenges

    We need to Initialize the Review

  • 7/25/2019 angular js level 01-05

    90/164

    We could do ng-in

    were better offcreating a controll

    by:

    Stars: {{review.stars}}

    {{review.body}}

    by: {{review.author}}

    1 star

    2 stars

    . . .

    Creating the Review Controller&

    ll ( i ll f i (){

  • 7/25/2019 angular js level 01-05

    91/164

    Now we need to upda

    all the Expressions this controller alias.

    ng-controller="ReviewController as review

  • 7/25/2019 angular js level 01-05

    92/164

    ng-controller="ReviewController as review

    reviewCtrl.

    reviewCtrl.

    reviewCtrl.

    reviewCtrl.

    reviewCtrl.

  • 7/25/2019 angular js level 01-05

    93/164

    ng-submit="reviewCtrl.addReview(product)

    We need

    this funct

    ng-submitallows us to call a function when the form is sub

    app.controller("ReviewController", function(){

    this.review ={};

    });

    reviewCtrl.

    reviewCtrl.

    reviewCtrl.

    ng-controller="ReviewController as revie

  • 7/25/2019 angular js level 01-05

    94/164

    reviewCtrl.

    ng-controller="ReviewController as revie

    reviewCtrl.

    reviewCtrl.

    ng-submit="reviewCtrl.addReview(product)

    Push reviewontproducts review

    app.controller("ReviewController", function(){

    this.review ={};

    });

    this.addReview =function(product) {

    product.reviews.push(this.review);

    };

  • 7/25/2019 angular js level 01-05

    95/164

    gbut the form s

    all previous va

    Resetting the Form on Submit

    app.controller("ReviewController", function(){

  • 7/25/2019 angular js level 01-05

    96/164

  • 7/25/2019 angular js level 01-05

    97/164

    Were not savreviews anyw

  • 7/25/2019 angular js level 01-05

    98/164

    Challenges

    Turns out Angular has some great client side validations w

    What about validations?

  • 7/25/2019 angular js level 01-05

    99/164

    Turns out Angular has some great client side validations w

    use with our directives.

    Our Old Form Code

  • 7/25/2019 angular js level 01-05

    100/164

  • 7/25/2019 angular js level 01-05

    101/164

    required

    no

    required

    Mark Requir

    Print ForreviewForm is {{reviewForm.$valid}}

  • 7/25/2019 angular js level 01-05

    102/164

    formwhen

  • 7/25/2019 angular js level 01-05

    103/164

    g

    ng-submit="reviewCtrl.addReview(product)

    We only want this method to be calledif reviewForm.$valid is true.

  • 7/25/2019 angular js level 01-05

    104/164

    ng-submit="reviewForm.$valid && reviewCtrl.addReview(product

    If valid is false, then addReviewisnever called.

    Doesnt Submit an Invalid Form

    H i ht i hi t

  • 7/25/2019 angular js level 01-05

    105/164

    How might we give a hint why their form is invalid?

    The Input Classes

  • 7/25/2019 angular js level 01-05

    106/164

    Source before typing email

    Source after typing, with invalid email

    Source after typing, with valid email

    So, lets highlight the form field using classes after we start typinshowing if a field is valid or invalid. ng-valid ng-invalid

    The classes

  • 7/25/2019 angular js level 01-05

    107/164

    .ng-invalid.ng-dirty{

    border-color:#FA787E;

    }

    .ng-valid.ng-dirty {

    border-color:#78FA89;

    }

    Red border for invalid

    Green border for valid

    Now with red and green borders!

  • 7/25/2019 angular js level 01-05

    108/164

    Web forms usually have rules around valid input:

    HTML5-based type validations

  • 7/25/2019 angular js level 01-05

    109/164

    Angular JS has built-in validations for common input ty

    min=1

    Can als

    & max

  • 7/25/2019 angular js level 01-05

    110/164

  • 7/25/2019 angular js level 01-05

    111/164

  • 7/25/2019 angular js level 01-05

    112/164

    Shaping Up with Angular JSLevel 4: Creating a Directive with an

    Associated Controller

    Decluttering our Code

  • 7/25/2019 angular js level 01-05

    113/164

    Were going to have multiple pages that want to reuse th

    snippet.

    How do we eliminate this duplication?

    g p g p p p

    {{product.name}}

    ${{product.price}}

    . . .

    Using ng-includefor Templates

  • 7/25/2019 angular js level 01-05

    114/164

    prod

    Awesome Multi-touch Keyboa

    $250.00

    ge

    ng-include is expecting a variable with the name of the fil

    To pass the name directly as a string, use single quotes ('

    l class l st g oup te g epeat p oduct sto e.p od

    {{product.name}}

    ${{product.price}}

  • 7/25/2019 angular js level 01-05

    115/164

    Web Server

    HTML Java

    Web Browser

    URL Request to server

  • 7/25/2019 angular js level 01-05

    116/164

    Response with Webpage & Assets

    HTML Returned

    Fetches ng-included file HTML

    Browser loadAngular ap

    Creating our First Custom Directive

    Using ng-include...

  • 7/25/2019 angular js level 01-05

    117/164

    Custom Directive

    Our old code and our custom Directive will do the same

    with some additional code.

    Why write a Directive?

    Why Directives?

    Directives allow you to write HTML that expresses the behof your application

  • 7/25/2019 angular js level 01-05

    118/164

    of your application.

    Canyoute

    ll

    thisdoe

    Template-expanding Directivesare the simplest:

    define a custom tag or attribute that is expanded or re

    Writing Custom Directives

  • 7/25/2019 angular js level 01-05

    119/164

    define a custom tag or attribute that is expanded or re

    can include Controller logic, if needed

    Directives can also be used for:

    Expressing complex UI

    Calling events and registering event handlers

    Reusing common components

    How to Build Custom Directives

  • 7/25/2019 angular js level 01-05

    120/164

    app.directive('productTitle', function(){ return{

    };

    });

    A configuration object defining how your directive will

    How to Build Custom Directives

    lC i J S i td h i HTML t l t t

  • 7/25/2019 angular js level 01-05

    121/164

    app.directive('productTitle', function(){ return{

    };

    });

    {{product.name}}

    $250.00

    Type of Directive(E for Element)

    Url of a template restrict:'E',

    templateUrl: 'product-title.html'

    camelCasein JavaScriptdashin HTML translates to ...

  • 7/25/2019 angular js level 01-05

    122/164

    Attribute vs Element Directives

    Element Directive

  • 7/25/2019 angular js level 01-05

    123/164

    Attribute Directive

    some browsers dont like self-

    Use Element Directives for UI widgets and Attribute Direcmixin behaviors... like a tooltip.

    Notice were not using a self-closing tag

    p /p

  • 7/25/2019 angular js level 01-05

    124/164

    {{product.name}} $250.00

    app.directive('productTitle', function(){

    return{

    };

    });

    Though normally attributes would be for mixin behaviors

    restrict: 'A',

    templateUrl:'product-title.html'Type of Directive(A for Attribute)

    When you think of a dynamic web application, do you thin

    youll be able to understand the functionality just by lookin

    Directives allow you to write better HTML

  • 7/25/2019 angular js level 01-05

    125/164

    youll be able to understand the functionality just by lookin

    the HTML?

    When you're writing an Angular JS application, you should

    able to understand the behavior and intent from just the H

    No, right?

    And youre likely using custom directives to write expre

    HTML.

  • 7/25/2019 angular js level 01-05

    126/164

    Shaping Up with Angular JS

    Creating Our Own Directives

    Reviewing our DirectiveTemplate-Expanding Directives

  • 7/25/2019 angular js level 01-05

    127/164

    {{product.name}}

    ${{product.price}}index.html

    An Attribute Directive

    An Element Directive

    What if we needa Controller?

    ng-controller="PanelController as panels"

    >

  • 7/25/2019 angular js level 01-05

    128/164

    inde

    . . .

    . . .

  • 7/25/2019 angular js level 01-05

    129/164

    produc

    >

  • 7/25/2019 angular js level 01-05

    130/164

    app.directive('productPanels', function(){

    return{

    restrict: 'E',

    templateUrl:'product-panels.html'

    };

    });

    What about the Controller?

    ng-controller="PanelController as panels"

    . . .

  • 7/25/2019 angular js level 01-05

    131/164

    app.directive('productPanels', function(){

    return{

    restrict: 'E',

    templateUrl:'product-panels.html'

    };

    });app.controller('PanelController', function(){

    . . .

    });

    First we need to move the functionality inside the direc

    Moving the Controller Inside

    ng-controller="PanelController as panels"

    . . .

  • 7/25/2019 angular js level 01-05

    132/164

    app.directive('productPanels', function(){

    return{

    restrict: 'E',

    templateUrl:'product-panels.html'

    };

    });

    ,

    Next, move the alias inside

    function(){

    . . .

    controller:

    }

    Need to Specify the Alias

    . . .

  • 7/25/2019 angular js level 01-05

    133/164

    Now it works, using panelsas our Controller Alia

    controllerAs: 'panels'

    };

    });

    app.directive('productPanels', function(){

    return{

    restrict: 'E',

    templateUrl:'product-panels.html'

    };

    });

    ,

    ,

    function(){

    . . .

    controller:

    }

  • 7/25/2019 angular js level 01-05

    134/164

  • 7/25/2019 angular js level 01-05

    135/164

    Challenges

  • 7/25/2019 angular js level 01-05

    136/164

  • 7/25/2019 angular js level 01-05

    137/164

  • 7/25/2019 angular js level 01-05

    138/164

    Shaping Up with Angular JSLevel 5: Dependencies and Services

    (function(){

    varapp=angular.module('store', [

    Starting to get a bit cluttered?

    app controller('StoreController' function(){ });

    ]);

  • 7/25/2019 angular js level 01-05

    139/164

    Can wethes

    . . .

    })();

    app.controller('StoreController',function(){ . . . });

    app.directive('productTitle',function(){ . . . });

    app.directive('productGallery',function(){ . . . });

    app.directive('productPanels',function(){ . . . });

    (function(){

    varapp=angular.module('store', [

    Extract into a new file ... products.js

    app controller('StoreController' function(){ });

    ]);

  • 7/25/2019 angular js level 01-05

    140/164

    . . .

    })();

    app.controller( StoreController ,function(){ . . . });

    app.directive('productTitle',function(){ .

    app.directive('productGallery',function(){

    app.directive('productPanels',function(){ .

    (function(){

    varapp=angular.module('store', [

    Make a newModule

    app controller('StoreController' function(){ });

    ]);

    Define a new for Produc

  • 7/25/2019 angular js level 01-05

    141/164

    . . .

    })();

    app.controller( StoreController ,function(){ . . . });

    (function(){

    varapp =angular.module('store

    })();

    -products',

    Mod

    Different closure

    means differentappvariable.

    app.directive('productTitle',function(){

    app.directive('productGallery',function()

    app.directive('productPanels',function(){

    (function(){

    varapp=angular.module('store', [

    Add it to the dependencies ...

    app.controller('StoreController', function(){ . . . });

    ]);'store-products'

    storedestore-p

  • 7/25/2019 angular js level 01-05

    142/164

    . . .

    })();

    app.controller( StoreController ,function(){ . . . });

    app.directive('productTitle',function(){

    app.directive('productGallery',function()

    app.directive('productPanels',function(){

    (function(){

    varapp =angular.module('store

    })();

    -products',

    Modu

    . . .

    Well also need to include the file

  • 7/25/2019 angular js level 01-05

    143/164

    . . .

  • 7/25/2019 angular js level 01-05

    144/164

    Best to split Modules around functionality:

    app.js top-level module attached via ng-app

    d j ll h f i li f d d

    How should I organize my application Mod

  • 7/25/2019 angular js level 01-05

    145/164

    products.js all the functionality for products and

  • 7/25/2019 angular js level 01-05

    146/164

    Challenges

    Does this feel strange?

    (function(){

    varapp=angular.module('store', ['store-products']);

    app controller('StoreController' function(){

  • 7/25/2019 angular js level 01-05

    147/164

    What is all this dat

    doing here?Where can we put it?

    Shouldn't we fetch this from an API?

    app.controller('StoreController',function(){

    this.products =[ {name:'. . .',price:1.99, . . . },

    {name:'. . .',price:1.99, . . . },

    {name:'. . .',price:1.99, . . . },

    . . .

    });

    })();

    . . .

    ];

    How do we get that data?

    (function(){

    varapp=angular.module('store', ['store-products']);

    app controller('StoreController' function(){

  • 7/25/2019 angular js level 01-05

    148/164

    How do we products fr

    http://api.example.com/products.json

    app.controller('StoreController',function(){

    this.products =???; . . .

    });

    })();

    {name:'. . .',price:1.99,

    {name:'. . .',price:1.99,

    {name:'. . .',price:1.99,

    [

    . . .

    ]

    Services give your Controller additional functionality, lik

    Fetching JSON data from a web service with $http

    Logging messages to the JavaScript console with $log

    We need a Service!

  • 7/25/2019 angular js level 01-05

    149/164

    Logging messages to the JavaScript console with $log

    Filtering an array with $filter

    All buista

    The $httpService is how we make an async request to a se

    By using $httpas a function with an options object:

    Introducing the$httpService!

    $http({ method: 'GET' url: '/products json' });

  • 7/25/2019 angular js level 01-05

    150/164

    Sohowdoweuseit?

    $http({method: GET , url: /products.json });

    Or using one of the shortcut methods:

    $http.get('/products.json', { apiKey:'myApiKey' });

    Both return a Promise object with .success()and .erro

    If we tell $httpto fetch JSON, the result will be automaticalldecoded into JavaScript objects and arrays

    Use this funky array syntax:

    app.controller('SomeController',['$http',function($http)

    How does a Controller use a Service like $

  • 7/25/2019 angular js level 01-05

    151/164

    } ]);

    Service nameService name as an argument

    Depe

    app.controller('SomeController', [ '$http','$log',function($htt

    } ]); If you needed more th

    When Angular is Loaded Services are Regis

    app.controller('SomeController', [ '$http','$log',function($htt

    } ]);

  • 7/25/2019 angular js level 01-05

    152/164

    SomeController

    Injector

    app.controller('SomeController', [ '$http','$log',function($ht

    } ]);

    A Controller is Initialized

  • 7/25/2019 angular js level 01-05

    153/164

    SomeController

    Injectorand $log,too ...I need $http...

    Psst... When I run...

    app.controller('SomeController', [ '$http','$log',function($ht

    } ]);

    Then When the Controller runs ...

  • 7/25/2019 angular js level 01-05

    154/164

    SomeController

    InjectorHere ya go!DependencyInjection!

    (function(){

    varapp=angular.module('store', [ 'store-products']);

    app.controller('StoreController',

    So where were we?

    ){function(

  • 7/25/2019 angular js level 01-05

    155/164

    this.products=???;);}

    })();

    (function(){

    varapp=angular.module('store', [ 'store-products']);

    app.controller('StoreController',

    Time for your injection!

    function(['$http', $htt

  • 7/25/2019 angular js level 01-05

    156/164

    this.products=???;);}

    })();

    StoreControllerneedsthe $httpService...

    ...sogets as an a

    Now what?

    ]

    (function(){

    varapp=angular.module('store', [ 'store-products']);

    app.controller('StoreController',

    Lets use our Service!

    function(['$http', $htt

  • 7/25/2019 angular js level 01-05

    157/164

    this.products=???;

    );}})();

    $http.get('/products.json')

    Fetch the contents ofproducts.json...

    ]

    (function(){

    varapp=angular.module('store', [ 'store-products']);

    app.controller('StoreController',

    Our Service will Return Data

    function(['$http', $htt

  • 7/25/2019 angular js level 01-05

    158/164

    this.products=???;

    );}

    })();

    $http.get('/products.json').success(function(data){

    ???

    });

    =data;

    $httpreturns a Pro

    success()gets theWhat do we assigndatato, though...?

    ]

    (function(){

    varapp=angular.module('store', [ 'store-products']);

    app.controller('StoreController',

    Storing the Data for use in our Page

    function(['$http', $htt

  • 7/25/2019 angular js level 01-05

    159/164

    );}

    })();

    $http.get('/products.json').success(function(data){

    });

    =data;

    varstore =this;

    store.products

    We need to store what thisis ...... and now we have soto put our dat

    But the page might look funny until the data loads.

    ]

    (function(){

    varapp=angular.module('store', [ 'store-products']);

    app.controller('StoreController',

    Initialize Products to be a Blank Array

    function(['$http', $htt

  • 7/25/2019 angular js level 01-05

    160/164

    );}

    })();

    $http.get('/products.json').success(function(data){

    });

    =data;

    varstore =this;

    store.products

    store.products =[];

    We need to initialize productsarray, since the page will render

    data returns from our get r]

  • 7/25/2019 angular js level 01-05

    161/164

    $http.post('/path/to/resource.json', { param:'value' });

    Additional $http functionalityIn addition to get()requests, $httpcan post(), put(), d

  • 7/25/2019 angular js level 01-05

    162/164

    $http({method:'PATCH', url:'/path/to/resource.json'});

    $http({method:'TRACE', url:'/path/to/resource.json' });

    $http.delete('/path/to/resource.json');

    $http({method:'OPTIONS', url: '/path/to/resource.json' })

    ...or any other HTTP method by using a configobject:

  • 7/25/2019 angular js level 01-05

    163/164

    Challenges

  • 7/25/2019 angular js level 01-05

    164/164