2014-03-24_01-35-50__module3.pdf
TRANSCRIPT
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
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
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
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
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
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
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
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
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
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
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
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
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"
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
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
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
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
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
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
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
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
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
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
8/17/2019 2014-03-24_01-35-50__Module3.pdf
http://slidepdf.com/reader/full/2014-03-2401-35-50module3pdf 24/26
Summary
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
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