2014-03-24_01-35-50__module3.pdf

26
8/17/2019 2014-03-24_01-35-50__Module3.pdf http://slidepdf.com/reader/full/2014-03-2401-35-50module3pdf 1/26 Modules, Controllers and Scope © Wahlin Consulting – All Rights Reserved ngularJS JumpStart

Upload: gutapetru13

Post on 06-Jul-2018

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 2014-03-24_01-35-50__Module3.pdf

8/17/2019 2014-03-24_01-35-50__Module3.pdf

http://slidepdf.com/reader/full/2014-03-2401-35-50module3pdf 1/26

Modules, Controllers

and Scope

© Wahlin Consulting – All Rights Reserved

ngularJS JumpStart

Page 2: 2014-03-24_01-35-50__Module3.pdf

8/17/2019 2014-03-24_01-35-50__Module3.pdf

http://slidepdf.com/reader/full/2014-03-2401-35-50module3pdf 2/26

AngularJS Architecture Patterns

 The Role of Controllers

 The ng-controller Directive

 The Role of Modules

Adding a Controller to a Module

Page 3: 2014-03-24_01-35-50__Module3.pdf

8/17/2019 2014-03-24_01-35-50__Module3.pdf

http://slidepdf.com/reader/full/2014-03-2401-35-50module3pdf 3/26

 View Controller

*FactoryDirectives

Routes

Module

Config

$scope

The Big Picture

Page 4: 2014-03-24_01-35-50__Module3.pdf

8/17/2019 2014-03-24_01-35-50__Module3.pdf

http://slidepdf.com/reader/full/2014-03-2401-35-50module3pdf 4/26

AngularJS Architecture Patterns

Page 5: 2014-03-24_01-35-50__Module3.pdf

8/17/2019 2014-03-24_01-35-50__Module3.pdf

http://slidepdf.com/reader/full/2014-03-2401-35-50module3pdf 5/26

AngularJS Architecture Patterns

AngularJS relies on two key architecture patterns:

Model-View-Controller (MVC)

Model-View-ViewModel (MVVM)

Patterns provide prescriptive guidance that can be

used to build applications

Page 6: 2014-03-24_01-35-50__Module3.pdf

8/17/2019 2014-03-24_01-35-50__Module3.pdf

http://slidepdf.com/reader/full/2014-03-2401-35-50module3pdf 6/26

$scope

MVC + MVVM = MV*

Controller

Model

Request

 ViewResponse

Page 7: 2014-03-24_01-35-50__Module3.pdf

8/17/2019 2014-03-24_01-35-50__Module3.pdf

http://slidepdf.com/reader/full/2014-03-2401-35-50module3pdf 7/26

The Role of Controllers

Page 8: 2014-03-24_01-35-50__Module3.pdf

8/17/2019 2014-03-24_01-35-50__Module3.pdf

http://slidepdf.com/reader/full/2014-03-2401-35-50module3pdf 8/26

AngularJS Controllers

Controllers act as the "brain" for a view:

Defines properties and methods Handles showing/hiding controls and data in a view

Handles events triggered by a view

Knows how to retrieve data Interacts with the view using the $scope object

Page 9: 2014-03-24_01-35-50__Module3.pdf

8/17/2019 2014-03-24_01-35-50__Module3.pdf

http://slidepdf.com/reader/full/2014-03-2401-35-50module3pdf 9/26

View Controller$scope

• $scope is "injected" into a controller

•  Acts as the ViewModel

•  Views bind to scope properties andfunctions

The Role of $scope

Page 10: 2014-03-24_01-35-50__Module3.pdf

8/17/2019 2014-03-24_01-35-50__Module3.pdf

http://slidepdf.com/reader/full/2014-03-2401-35-50module3pdf 10/26

<script>

function   SimpleController($scope) {

$scope.customers = [

{ name: 'Danny Wallace', city: 'Phoenix'   },

name: 

'Jamie 

Riley', 

city: 

'Atlanta'   },{ name: 'Heedy Thomas', city: 'Chandler'   },

{ name: 'Jeff James', city: 'Seattle'   }

];

}

</script>

$scope injecteddynamically

Creating a Controller

Page 11: 2014-03-24_01-35-50__Module3.pdf

8/17/2019 2014-03-24_01-35-50__Module3.pdf

http://slidepdf.com/reader/full/2014-03-2401-35-50module3pdf 11/26

The ng-controller Directive

Page 12: 2014-03-24_01-35-50__Module3.pdf

8/17/2019 2014-03-24_01-35-50__Module3.pdf

http://slidepdf.com/reader/full/2014-03-2401-35-50module3pdf 12/26

<div   class="container"   data‐ng‐controller="SimpleController">

<h3>Adding a Simple Controller</h3>

<ul>

<li   data‐

ng‐

repeat="cust 

in 

customers">{{ cust.name }}  ‐  {{ cust.city }}

</li>

</ul>

</div>

<script>

function   SimpleController($scope) {

$scope.customers = [

{ name: 'Dave Jones', city: 'Phoenix'   },

name: 

'Jamie 

Riley', 

city: 

'Atlanta'   },{ name: 'Heedy Wahlin', city: 'Chandler'   },

{ name: 'Thomas Winter', city: 'Seattle'   }

];

}

</script>

Define the controller

to use

Tying a View and Controller Together

Page 13: 2014-03-24_01-35-50__Module3.pdf

8/17/2019 2014-03-24_01-35-50__Module3.pdf

http://slidepdf.com/reader/full/2014-03-2401-35-50module3pdf 13/26

<div   class="container"   data‐ng‐controller="SimpleController as ctrl">

<h3>Adding a Simple Controller</h3>

<ul>

<li   data‐

ng‐

repeat="cust 

in 

ctrl.customers">{{ cust.name }}  ‐  {{ cust.city }}

</li>

</ul>

</div>

<script>

function   SimpleController() {

this.customers = [

{ name: 'Dave Jones', city: 'Phoenix'   },

name: 

'Jamie 

Riley', 

city: 

'Atlanta'   },{ name: 'Heedy Wahlin', city: 'Chandler'   },

{ name: 'Thomas Winter', city: 'Seattle'   }

];

}

</script>

Using "controller as"

Page 14: 2014-03-24_01-35-50__Module3.pdf

8/17/2019 2014-03-24_01-35-50__Module3.pdf

http://slidepdf.com/reader/full/2014-03-2401-35-50module3pdf 14/26

The Role of Modules

Page 15: 2014-03-24_01-35-50__Module3.pdf

8/17/2019 2014-03-24_01-35-50__Module3.pdf

http://slidepdf.com/reader/full/2014-03-2401-35-50module3pdf 15/26

What is a Module?

Modules are containers for:

Controllers

Routes

Factories/Services

Directives

Filters

Module

Page 16: 2014-03-24_01-35-50__Module3.pdf

8/17/2019 2014-03-24_01-35-50__Module3.pdf

http://slidepdf.com/reader/full/2014-03-2401-35-50module3pdf 16/26

ControllerFactoryDirective

Routes

Module

Config

Service

<html   ng‐app="moduleName">

Modules are Containers

Filter

Page 17: 2014-03-24_01-35-50__Module3.pdf

8/17/2019 2014-03-24_01-35-50__Module3.pdf

http://slidepdf.com/reader/full/2014-03-2401-35-50module3pdf 17/26

Create a module using angular.module():

var   demoApp = angular.module('demoApp', []);

Defining a Module

Dependencies

Page 18: 2014-03-24_01-35-50__Module3.pdf

8/17/2019 2014-03-24_01-35-50__Module3.pdf

http://slidepdf.com/reader/full/2014-03-2401-35-50module3pdf 18/26

Modules may rely on functionality from other

modules

Helper modules can be "injected" into a module:

Injecting Dependencies into a Module

var   demoApp = angular.module('demoApp', 

['helperModule']);

External module

Page 19: 2014-03-24_01-35-50__Module3.pdf

8/17/2019 2014-03-24_01-35-50__Module3.pdf

http://slidepdf.com/reader/full/2014-03-2401-35-50module3pdf 19/26

Adding a Controller to a Module

Page 20: 2014-03-24_01-35-50__Module3.pdf

8/17/2019 2014-03-24_01-35-50__Module3.pdf

http://slidepdf.com/reader/full/2014-03-2401-35-50module3pdf 20/26

var   demoApp = angular.module('demoApp', []);

demoApp.controller('SimpleController', 

function   ($scope) 

{$scope.customers = [

{ name: 'Dave Jones', city: 'Phoenix'   },

{ name: 'Jamie Riley', city: 'Atlanta'   },

{ name: 'Heedy Wahlin', city: 'Chandler'   },

name: 

'Thomas 

Winter', 

city: 

'Seattle'   }];

});

Define a Module

Define a Controller

Adding a Controller to a Module: Option 1

Page 21: 2014-03-24_01-35-50__Module3.pdf

8/17/2019 2014-03-24_01-35-50__Module3.pdf

http://slidepdf.com/reader/full/2014-03-2401-35-50module3pdf 21/26

var   demoApp = angular.module('demoApp', []);

angular.module('demoApp').controller('SimpleController', 

function   ($scope) {

$scope.customers = [

{ name: 'Dave Jones', city: 'Phoenix'   },

{ name: 'Jamie Riley', city: 'Atlanta'   },

name: 

'Heedy 

Wahlin', 

city: 

'Chandler'   },{ name: 'Thomas Winter', city: 'Seattle'   }

];

});

Define a Module

Reference module

Adding a Controller to a Module: Option 2

Page 22: 2014-03-24_01-35-50__Module3.pdf

8/17/2019 2014-03-24_01-35-50__Module3.pdf

http://slidepdf.com/reader/full/2014-03-2401-35-50module3pdf 22/26

var   demoApp = angular.module('demoApp', []);

...

(function() {

var   SimpleController = 

function   ($scope) 

{$scope.customers = [

{ name: 'Dave Jones', city: 'Phoenix'   },

{ name: 'Jamie Riley', city: 'Atlanta'   }

];

};

angular.module('demoApp')

.controller('SimpleController',SimpleController);

}());

Adding a Controller to a Module: Option 3

Page 23: 2014-03-24_01-35-50__Module3.pdf

8/17/2019 2014-03-24_01-35-50__Module3.pdf

http://slidepdf.com/reader/full/2014-03-2401-35-50module3pdf 23/26

angular.module('demoApp')

.controller('SimpleController', ['$scope',   function ($scope) {

$scope.customers = 

[…];}]);

OR

var SimpleController = function ($scope) {

$scope.customers = […];

};

SimpleController.$inject 

['$scope'];

angular.module('demoApp')

.controller('SimpleController', SimpleController);

Dealing with Minification

Page 24: 2014-03-24_01-35-50__Module3.pdf

8/17/2019 2014-03-24_01-35-50__Module3.pdf

http://slidepdf.com/reader/full/2014-03-2401-35-50module3pdf 24/26

Summary

Page 25: 2014-03-24_01-35-50__Module3.pdf

8/17/2019 2014-03-24_01-35-50__Module3.pdf

http://slidepdf.com/reader/full/2014-03-2401-35-50module3pdf 25/26

 View Controller

*FactoryDirectives

Routes

Module

Config

$scope

The Big Picture

Page 26: 2014-03-24_01-35-50__Module3.pdf

8/17/2019 2014-03-24_01-35-50__Module3.pdf

http://slidepdf.com/reader/full/2014-03-2401-35-50module3pdf 26/26

Summary

AngularJS relies on the MVC and MVVM architectural

patterns

Controllers act as the "brain" for a view:

Know how to retrieve and store data

Interact with views using $scope

Modules act as containers for AngularJS applications

© Wahlin Consulting – All Rights Reserved