SlideShare a Scribd company logo
Modules and injector
angular
Modules (private)
angular.module( 'moduleName' , [ ] ) angular.module( 'moduleName' )
moduleName
<html lang="en" ng-app="myTodo">
<body>
...
<script src="lib/angular.js"></script>
<!-- My Modules -->
<script src="js/app.js"></script>
<script src="js/controllers.js"></script>
</body>
</html>
// app.js file must to be first
angular.module('myTodo',[]);
// controllers.js must to be after app.js
angular.module('myTodo');
angular.module('myApp', ['bl'])
// Name (key) and value (function)
.controller('MainCtrl',MainCtrl)
.directive ('tabs',tabsDirective)
.provider('proxy',proxyProvider)
// Functions only
.config(function(proxyProvider){…})
.run(function(proxy){…});
 name : 'myApp'
 requires : [ 'bl']
_runBlocks : [ ]
_configBlocks : [ ]
_invokeQueue : [ ]
'MainCtrl',MainCtrl
'tabs',tabsDirective
'proxy',proxyProvider
function(proxyProvider){…}
function(proxy){…}
 name : string
 requires : [ ]
_runBlocks : [ ]
run( initializationFn )
_configBlocks : [ ]
Config(function( xxxProvider ){ })
_invokeQueue : [ ]
- controller(name, constructor)
- directive (name, directiveFn)
- filter (name, filterFactory)
- animation (name, animationFactory)
- service (name, constructor)
- factory (name, providerFn)
- provider (name, providerType)
- decorator(name, fn )
- constant (name, object)
- value (name, object)
angular
Modules (private)
Browser
<html lang="en" ng-app="app">
<body>
<div class="view-container">
<div ng-view class="view-frame"></div>
</div>
<script src="lib/angular.js"></script>
<!-- Angular Modules -->
<script src="lib/angular-route.js"></script>
<!-- My Modules -->
<script src="js/app.js"></script>
<script src="js/controllers.js"></script>
<script src="js/services.js"></script>
</body>
</html>
ngRoute app
ctrls services
DOM Content
Loaded Event
Modules and injector
var injector = createInjector(modules);
injector.invoke([
'$rootScope','$rootElement','$compile','$injector','$animate',
function(scope, element, compile, injector, animate) {
scope.$apply(function() {
element.data('$injector', injector);
compile(element)(scope);
});
}]
);
Create the injector and
load the modules to
the injector.
Compile the root
element and return
link function.
Execute the link
function with the root
scope.
Apply,
update
the page
angular
Modules (private)
ngRoute app
ctrls services
$injector (Instance Injector)
Provider Injector
Instance Cache
Provider Cache
Keys Values
Invoke( )get( )
Keys Values
instantiate( ) has( )
Invoke( )get( ) instantiate( ) has( )
angular
Modules (private)
ngRoute
app
ctrls
services
<!-- app.js -->
angular.module('app',['ngRoute','ctrls'])
.config(function($routeProvider){});
<!-- angular-route.js -->
angular.module('ngRoute',[])
.provider('$route',fn);
<!-- controllers.js -->
angular.module('ctrls',['services'])
.controller('MainCtrl', fn);
<!-- services.js -->
angular.module('services',[])
.service('todoSer', fn);
<html lang="en" ng-app="app">
angular
Modules (private)
ngRoute
app
services
Provider Injector
Keys Values
todoSer
{ $get : factoryFn }
$controllerProvider
Keys Values
constructor
$route
{ $get : factoryFn }
config
ctrls
MainCtrl
{ $get : factoryFn }
todoSer
Provider
$route
Provider
angular
$injector
Provider Injector
Instance Cache
Provider Cache
Keys Values
{ $get : factoryFn }
{ $get : factoryFn }
todoSerProvider
$routeProvider
Invoke( )get( )
Keys Values
instantiate( ) has( )
Invoke( )get( ) instantiate( ) has( )
angular
Provider Injector
Instance Cache
Provider Cache
Keys Values
{ $get : factoryFn }
{ $get : factoryFn }
todoSerProvider
$routeProvider
angular.module('app',['ngRoute','ctrls']
.run(function runFn(todoSer){ todoSer.load(); });
Invoke( runFn )get( 'todoSer' )
get( 'todoSer' + 'Provider' )
Keys Values
{ . . .}todoSer
ngXXX
angular.module('myApp', ['ngXXX', 'ngYYY']);
Invoke
Queue
ngYYY
Invoke
Queue
myApp
Invoke
Queue
Config
blocks
Config
blocks
Config
blocks
Run
blocks
Run
blocks
Run
blocks
$injector
Instance
Cache
Provider
Cache
// You write functions such as this one.
function doSomething(serviceA, serviceB) {
// do something here.
}
// Angular provides the injector for your application
var $injector = ...;
$injector.invoke(doSomething);
var serviceA = $injector.get('serviceA');
var serviceB = $injector.get('serviceB');
doSomething(serviceA, serviceB);
// inline option
$injector.invoke( [ 'serviceA', function (serviceA) { } ] );
$injector.invoke( function (serviceA) { } );
$injector.invoke( function ( sa ) { } );
minified
xxx.js
xxx.min.js
// annotated option
explicit.$inject = ['serviceA'];
function explicit(serviceA) { };
$injector.invoke(explicit);
<ANY ng-app="angular.Module" [ng-strict-di="boolean"]>
...
</ANY>
Useful for debugging info, will assist in
tracking down the root of these bugs.
<html lang="en" ng-app="app">
<body>
<script src="lib/angular.js"></script>
<script src="lib/angular-route.js"></script>
<script src="js/app.js"></script>
</body>
</html>
angular.module('app', ['ngRoute']);
The module file are
being downloaded
correctly
The module name
in the requires is
not misspelled
<html lang="en" ng-app="app">
<body ng-controller="Ctrl">
<script src="angular.js"></script>
<script src="angular-route.js"></script>
<script src="app.js"></script>
</body>
</html>
angular.module('app', ['ngRoute']).controller('Ctrl', function(){...});
The module, where
the provider /
controller is
defined, is loaded
to the injector
Spelled
correctly
Modules and injector
HTML
Browser
Static
DOM
Dynamic
DOM
(View)
AngularJS
DOM
Content
Loaded
Event
ng-app=“module”
$injector
$compile $rootScope
$compile (dom,
$rootScope)

More Related Content

PPTX
Angular 2 Architecture (Bucharest 26/10/2016)
PPTX
Angular 2.0 forms
PPTX
Http Communication in Angular 2.0
PPTX
Upgrading from Angular 1.x to Angular 2.x
PPTX
Performance Optimization In Angular 2
PPTX
Angular 2 - Ahead of-time Compilation
PPTX
Routing And Navigation
PPTX
AngularJS Internal
Angular 2 Architecture (Bucharest 26/10/2016)
Angular 2.0 forms
Http Communication in Angular 2.0
Upgrading from Angular 1.x to Angular 2.x
Performance Optimization In Angular 2
Angular 2 - Ahead of-time Compilation
Routing And Navigation
AngularJS Internal

What's hot (20)

PPTX
Template syntax in Angular 2.0
PPTX
AngularJS $Provide Service
PDF
Databinding and Performance-Tuning in Angular 2
PPTX
AngularJS Compile Process
PPTX
Angular 2.0 Dependency injection
PPTX
Angular 1.x vs. Angular 2.x
PPTX
Angular 2.0 Routing and Navigation
PPTX
Angular 2 Architecture
PPTX
AngularJs $provide API internals & circular dependency problem.
PPTX
Angular 2 NgModule
PPTX
AngularJS Directives
PPTX
AngularJS Architecture
PDF
Angular js routing options
PPTX
AngularJs
PDF
AngularJS Framework
PDF
AngularJS Basics with Example
PPTX
Building an End-to-End AngularJS Application
PPTX
AngularJS Services
PPTX
Practical AngularJS
PDF
준비하세요 Angular js 2.0
Template syntax in Angular 2.0
AngularJS $Provide Service
Databinding and Performance-Tuning in Angular 2
AngularJS Compile Process
Angular 2.0 Dependency injection
Angular 1.x vs. Angular 2.x
Angular 2.0 Routing and Navigation
Angular 2 Architecture
AngularJs $provide API internals & circular dependency problem.
Angular 2 NgModule
AngularJS Directives
AngularJS Architecture
Angular js routing options
AngularJs
AngularJS Framework
AngularJS Basics with Example
Building an End-to-End AngularJS Application
AngularJS Services
Practical AngularJS
준비하세요 Angular js 2.0
Ad

Viewers also liked (17)

PPTX
Node.js File system & Streams
PPTX
Node.js Socket.IO
PPTX
Node js overview
PPTX
Node.js Event Emitter
PPTX
Async & Parallel in JavaScript
PPTX
Angular 2.0 Views
PPTX
Angular 2.0 Pipes
PDF
Nodejs
PPTX
Node.js Spplication Scaling
PPTX
Component lifecycle hooks in Angular 2.0
PPTX
Modules in ECMAScript 6.0
PPTX
AngularJS Routing
PDF
Making use of OpenStreetMap data with Python
PDF
Rachel's grandmother's recipes
PPT
ECMAScript 6: A Better JavaScript for the Ambient Computing Era
PDF
ES2015 (ES6) Overview
PPTX
OpenStreetMap in 3D using Python
Node.js File system & Streams
Node.js Socket.IO
Node js overview
Node.js Event Emitter
Async & Parallel in JavaScript
Angular 2.0 Views
Angular 2.0 Pipes
Nodejs
Node.js Spplication Scaling
Component lifecycle hooks in Angular 2.0
Modules in ECMAScript 6.0
AngularJS Routing
Making use of OpenStreetMap data with Python
Rachel's grandmother's recipes
ECMAScript 6: A Better JavaScript for the Ambient Computing Era
ES2015 (ES6) Overview
OpenStreetMap in 3D using Python
Ad

Similar to Modules and injector (20)

PDF
"Angular.js Concepts in Depth" by Aleksandar Simović
PPTX
Angular Workshop_Sarajevo2
PDF
AngularJs
PPTX
Angular2 + rxjs
PDF
Opinionated AngularJS
PDF
Clean Javascript
PPTX
Protractor framework architecture with example
PDF
Technozaure - Angular2
PDF
Symfony2 from the Trenches
PDF
Angular2 - In Action
PDF
A Story about AngularJS modularization development
PDF
Symfony2 - from the trenches
PPTX
AngularJs-training
PPT
We sport architecture_implementation
PDF
Exploring Angular 2 - Episode 1
PDF
ChtiJUG - Introduction à Angular2
PPTX
Top 10 Mistakes AngularJS Developers Make
PDF
Angular를 활용한 웹 프론트단 개발과 2.0에서 달라진점
PDF
Angular를 활용한 웹 프론트단 개발과 2.0에서 달라진점
PPTX
Angular js
"Angular.js Concepts in Depth" by Aleksandar Simović
Angular Workshop_Sarajevo2
AngularJs
Angular2 + rxjs
Opinionated AngularJS
Clean Javascript
Protractor framework architecture with example
Technozaure - Angular2
Symfony2 from the Trenches
Angular2 - In Action
A Story about AngularJS modularization development
Symfony2 - from the trenches
AngularJs-training
We sport architecture_implementation
Exploring Angular 2 - Episode 1
ChtiJUG - Introduction à Angular2
Top 10 Mistakes AngularJS Developers Make
Angular를 활용한 웹 프론트단 개발과 2.0에서 달라진점
Angular를 활용한 웹 프론트단 개발과 2.0에서 달라진점
Angular js

More from Eyal Vardi (8)

PPTX
Why magic
PPTX
Smart Contract
PPTX
Proxies in ECMAScript 6.0
PPTX
Iterators & Generators in ECMAScript 6.0
PPTX
Symbols in ECMAScript 6.0
PPTX
Objects & Classes in ECMAScript 6.0
PPTX
Scope & Functions in ECMAScript 6.0
PPTX
Node.js Express
Why magic
Smart Contract
Proxies in ECMAScript 6.0
Iterators & Generators in ECMAScript 6.0
Symbols in ECMAScript 6.0
Objects & Classes in ECMAScript 6.0
Scope & Functions in ECMAScript 6.0
Node.js Express

Recently uploaded (20)

PDF
cuic standard and advanced reporting.pdf
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Spectral efficient network and resource selection model in 5G networks
PPTX
Big Data Technologies - Introduction.pptx
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
KodekX | Application Modernization Development
PDF
Modernizing your data center with Dell and AMD
PDF
Electronic commerce courselecture one. Pdf
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
NewMind AI Monthly Chronicles - July 2025
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPTX
Cloud computing and distributed systems.
cuic standard and advanced reporting.pdf
Understanding_Digital_Forensics_Presentation.pptx
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Unlocking AI with Model Context Protocol (MCP)
Spectral efficient network and resource selection model in 5G networks
Big Data Technologies - Introduction.pptx
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
“AI and Expert System Decision Support & Business Intelligence Systems”
KodekX | Application Modernization Development
Modernizing your data center with Dell and AMD
Electronic commerce courselecture one. Pdf
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Advanced methodologies resolving dimensionality complications for autism neur...
Per capita expenditure prediction using model stacking based on satellite ima...
NewMind AI Monthly Chronicles - July 2025
Network Security Unit 5.pdf for BCA BBA.
Dropbox Q2 2025 Financial Results & Investor Presentation
Cloud computing and distributed systems.

Modules and injector

  • 2. angular Modules (private) angular.module( 'moduleName' , [ ] ) angular.module( 'moduleName' ) moduleName
  • 3. <html lang="en" ng-app="myTodo"> <body> ... <script src="lib/angular.js"></script> <!-- My Modules --> <script src="js/app.js"></script> <script src="js/controllers.js"></script> </body> </html> // app.js file must to be first angular.module('myTodo',[]); // controllers.js must to be after app.js angular.module('myTodo');
  • 4. angular.module('myApp', ['bl']) // Name (key) and value (function) .controller('MainCtrl',MainCtrl) .directive ('tabs',tabsDirective) .provider('proxy',proxyProvider) // Functions only .config(function(proxyProvider){…}) .run(function(proxy){…});  name : 'myApp'  requires : [ 'bl'] _runBlocks : [ ] _configBlocks : [ ] _invokeQueue : [ ] 'MainCtrl',MainCtrl 'tabs',tabsDirective 'proxy',proxyProvider function(proxyProvider){…} function(proxy){…}
  • 5.  name : string  requires : [ ] _runBlocks : [ ] run( initializationFn ) _configBlocks : [ ] Config(function( xxxProvider ){ }) _invokeQueue : [ ] - controller(name, constructor) - directive (name, directiveFn) - filter (name, filterFactory) - animation (name, animationFactory) - service (name, constructor) - factory (name, providerFn) - provider (name, providerType) - decorator(name, fn ) - constant (name, object) - value (name, object)
  • 6. angular Modules (private) Browser <html lang="en" ng-app="app"> <body> <div class="view-container"> <div ng-view class="view-frame"></div> </div> <script src="lib/angular.js"></script> <!-- Angular Modules --> <script src="lib/angular-route.js"></script> <!-- My Modules --> <script src="js/app.js"></script> <script src="js/controllers.js"></script> <script src="js/services.js"></script> </body> </html> ngRoute app ctrls services DOM Content Loaded Event
  • 8. var injector = createInjector(modules); injector.invoke([ '$rootScope','$rootElement','$compile','$injector','$animate', function(scope, element, compile, injector, animate) { scope.$apply(function() { element.data('$injector', injector); compile(element)(scope); }); }] ); Create the injector and load the modules to the injector. Compile the root element and return link function. Execute the link function with the root scope. Apply, update the page
  • 9. angular Modules (private) ngRoute app ctrls services $injector (Instance Injector) Provider Injector Instance Cache Provider Cache Keys Values Invoke( )get( ) Keys Values instantiate( ) has( ) Invoke( )get( ) instantiate( ) has( )
  • 10. angular Modules (private) ngRoute app ctrls services <!-- app.js --> angular.module('app',['ngRoute','ctrls']) .config(function($routeProvider){}); <!-- angular-route.js --> angular.module('ngRoute',[]) .provider('$route',fn); <!-- controllers.js --> angular.module('ctrls',['services']) .controller('MainCtrl', fn); <!-- services.js --> angular.module('services',[]) .service('todoSer', fn); <html lang="en" ng-app="app">
  • 11. angular Modules (private) ngRoute app services Provider Injector Keys Values todoSer { $get : factoryFn } $controllerProvider Keys Values constructor $route { $get : factoryFn } config ctrls MainCtrl { $get : factoryFn } todoSer Provider $route Provider
  • 12. angular $injector Provider Injector Instance Cache Provider Cache Keys Values { $get : factoryFn } { $get : factoryFn } todoSerProvider $routeProvider Invoke( )get( ) Keys Values instantiate( ) has( ) Invoke( )get( ) instantiate( ) has( )
  • 13. angular Provider Injector Instance Cache Provider Cache Keys Values { $get : factoryFn } { $get : factoryFn } todoSerProvider $routeProvider angular.module('app',['ngRoute','ctrls'] .run(function runFn(todoSer){ todoSer.load(); }); Invoke( runFn )get( 'todoSer' ) get( 'todoSer' + 'Provider' ) Keys Values { . . .}todoSer
  • 15. // You write functions such as this one. function doSomething(serviceA, serviceB) { // do something here. } // Angular provides the injector for your application var $injector = ...; $injector.invoke(doSomething); var serviceA = $injector.get('serviceA'); var serviceB = $injector.get('serviceB'); doSomething(serviceA, serviceB);
  • 16. // inline option $injector.invoke( [ 'serviceA', function (serviceA) { } ] ); $injector.invoke( function (serviceA) { } ); $injector.invoke( function ( sa ) { } ); minified xxx.js xxx.min.js // annotated option explicit.$inject = ['serviceA']; function explicit(serviceA) { }; $injector.invoke(explicit);
  • 17. <ANY ng-app="angular.Module" [ng-strict-di="boolean"]> ... </ANY> Useful for debugging info, will assist in tracking down the root of these bugs.
  • 18. <html lang="en" ng-app="app"> <body> <script src="lib/angular.js"></script> <script src="lib/angular-route.js"></script> <script src="js/app.js"></script> </body> </html> angular.module('app', ['ngRoute']); The module file are being downloaded correctly The module name in the requires is not misspelled
  • 19. <html lang="en" ng-app="app"> <body ng-controller="Ctrl"> <script src="angular.js"></script> <script src="angular-route.js"></script> <script src="app.js"></script> </body> </html> angular.module('app', ['ngRoute']).controller('Ctrl', function(){...}); The module, where the provider / controller is defined, is loaded to the injector Spelled correctly