Share
Embed
The magic of Angular Dependency Injection
Talk at AngularJS Boston Meetup April 3, 2013
Alex Rothenberg
April 04, 2013
Annual Pro sale · Save $30
Have presentations of your own?
Share with private links, password protection, and more control over your work.
Transcript
93 slides
-
AngularJS Boston Meetup April 3, 2013 Alex Rothenberg http://www.alexrothenberg.com @alexrothenberg
https://github.com/alexrothenberg McKinsey & Co. The Magic of Angular Dependency Injection Thursday, April 4, 13 -
var MyController = function($scope, $filter, $location, $timeout, $http, $routeParams){ //
start using these services } Thursday, April 4, 13 -
var MyController = function() { var $scope = new Scope()
var $location = LocationFactory.gimme() // load the rest... // now do some work } Thursday, April 4, 13 -
This is the Angular Way var MyController = function($scope, $filter,
$location, $timeout, $http, $routeParams){ // start using these services } Thursday, April 4, 13 -
1. Don’t hardcode your dependencies 2. Let your caller pass
them in What is Dependency Injection? Thursday, April 4, 13 -
var onClick = function() { $.get("https://api.github.com", function(data) { $('#res').html(data.emojis_url); }
); } $('#my-button').click(onClick); Thursday, April 4, 13 -
var onClick = function() { $.get("https://api.github.com", function(data) { $('#res').html(data.emojis_url); }
); } $('#my-button').click(onClick); Thursday, April 4, 13 -
it("should get the gihub emoji", function() { onClick() $('#res').html().should.equal('something') });
Oh no do we have to setup a DOM Thursday, April 4, 13 -
it("should get the gihub emoji", function() { onClick() $('#res').html().should.equal('something') });
Where did “something” come from? Thursday, April 4, 13 -
it("should get the gihub emoji", function() { onClick() fakeDom.should.have.called('html') .with('something')
}); Use a fakeDOM Thursday, April 4, 13 -
it("should get the gihub emoji", function() { fakeAjax = mock
fakeAjax.expectGET('https://api.github.com') .respond({ "emojis_url": "something" }); onClick() fakeDom.should.have.called('html') .with('something') }); Set “something” Expect “something” Thursday, April 4, 13 -
it("should get the gihub emoji", function() { fakeAjax = mock
fakeAjax.expectGET('https://api.github.com') .respond({ "emojis_url": "something" }); onClick(fakeDom, fakeAjax) fakeDom.should.have.called('html') .with('something') }); Use fake services Thursday, April 4, 13 -
var onClick = function() { $.get("https://api.github.com", function(data) { $('#res').html(data.emojis_url); }
); } $('#my-button').click(onClick); Thursday, April 4, 13 -
var onClick = function(ajaxer, domer) { ajaxer.get("https://api.github.com", function(data) { domer('#res').html(data.emojis_url);
} ); } $('#my-button').click(onClick); Thursday, April 4, 13 -
var onClick = function(ajaxer, domer) { ajaxer.get("https://api.github.com", function(data) { domer('#res').html(data.emojis_url);
} ); } $('#my-button').click(onClick); How is jQuery passed in? Thursday, April 4, 13 -
<html ng-app> <head> <script src="angular.js"></script> <script> var MyController = function($scope)
{ $scope.name = 'World' } </script> </head> <body ng-controller="MyController"> Hello {{ name }} </body> </html> Thursday, April 4, 13 -
<html ng-app> <head> <script src="angular.js"></script> <script> var MyController = function($scope)
{ $scope.name = 'World' } </script> </head> <body ng-controller="MyController"> Hello {{ name }} </body> </html> Thursday, April 4, 13 -
<html ng-app> <head> <script src="angular.js"></script> <script> var MyController = function($scope)
{ $scope.name = 'World' } </script> </head> <body ng-controller="MyController"> Hello {{ name }} </body> </html> Thursday, April 4, 13 -
<html ng-app> <head> <script src="angular.js"></script> <script> var MyController = function($scope)
{ $scope.name = 'World' } </script> </head> <body ng-controller="MyController"> Hello {{ name }} </body> </html> Thursday, April 4, 13 -
var scope, ctrl; beforeEach(inject(function($rootScope, $controller) { scope = $rootScope.$new(); ctrl
= $controller(MyController, {$scope: scope}); })); it('should set name to "World"', function() { expect(scope.name).toEqual('World'); }); Thursday, April 4, 13 -
var scope, ctrl; beforeEach(inject(function($rootScope, $controller) { scope = $rootScope.$new(); ctrl
= $controller(MyController, {$scope: scope}); })); it('should set name to "World"', function() { expect(scope.name).toEqual('World'); }); Thursday, April 4, 13 -
var scope, ctrl; beforeEach(inject(function($rootScope, $controller) { scope = $rootScope.$new(); ctrl
= $controller(MyController, {$scope: scope}); })); it('should set name to "World"', function() { expect(scope.name).toEqual('World'); }); Thursday, April 4, 13 -
var scope, ctrl; beforeEach(inject(function($rootScope, $controller) { scope = $rootScope.$new(); ctrl
= $controller(MyController, {$scope: scope}); })); it('should set name to "World"', function() { expect(scope.name).toEqual('World'); }); Thursday, April 4, 13 -
Injectable The dependency injection in AngularJS allows you to declaratively
describe how your application is wired. This means that your application needs no main() method which is usually an unmaintainable mess. Dependency injection is also a core to AngularJS. This means that any component which does not fit your needs can easily be replaced. Testable AngularJS was designed from ground up to be testable. It encourages behavior-view separation, comes pre-bundled with mocks, and takes full advantage of dependency injection. It also comes with end-to-end scenario runner which eliminates test flakiness by understanding the inner workings of AngularJS. http://angularjs.org/ Thursday, April 4, 13 -
Injectable The dependency injection in AngularJS allows you to declaratively
describe how your application is wired. This means that your application needs no main() method which is usually an unmaintainable mess. Dependency injection is also a core to AngularJS. This means that any component which does not fit your needs can easily be replaced. Testable AngularJS was designed from ground up to be testable. It encourages behavior-view separation, comes pre-bundled with mocks, and takes full advantage of dependency injection. It also comes with end-to-end scenario runner which eliminates test flakiness by understanding the inner workings of AngularJS. http://angularjs.org/ Dependency injection is also core to AngularJS. Thursday, April 4, 13 -
Injectable The dependency injection in AngularJS allows you to declaratively
describe how your application is wired. This means that your application needs no main() method which is usually an unmaintainable mess. Dependency injection is also a core to AngularJS. This means that any component which does not fit your needs can easily be replaced. Testable AngularJS was designed from ground up to be testable. It encourages behavior-view separation, comes pre-bundled with mocks, and takes full advantage of dependency injection. It also comes with end-to-end scenario runner which eliminates test flakiness by understanding the inner workings of AngularJS. http://angularjs.org/ AngularJS was designed from ground up to be testable Thursday, April 4, 13 -
1. What is Dependency Injection 2. Why Dependency Injection is
useful 3. How we use Dependency Injection in Angular 4. How Angular implements DI 5. Use our knowledge to break Angular 6. Thursday, April 4, 13 -
1. What is Dependency Injection 2. Why Dependency Injection is
useful 3. How we use Dependency Injection in Angular 4. How Angular implements DI 5. Use our knowledge to break Angular 6. We’ll unbreak it Thursday, April 4, 13 -
<body ng-controller="MyController"> <h1>Recent commits to AngularJS</h1> <ul> <li ng-repeat="commit in
commits"> {{ commit.commit.committer.date | date}} {{ commit.sha }} {{commit.commit.message}} </li> </ul> </body> </html> Thursday, April 4, 13 -
<body ng-controller="MyController"> <h1>Recent commits to AngularJS</h1> <ul> <li ng-repeat="commit in
commits"> {{ commit.commit.committer.date | date}} {{ commit.sha }} {{commit.commit.message}} </li> </ul> </body> </html> Thursday, April 4, 13 -
<body ng-controller="MyController"> <h1>Recent commits to AngularJS</h1> <ul> <li ng-repeat="commit in
commits"> {{ commit.commit.committer.date | date}} {{ commit.sha }} {{commit.commit.message}} </li> </ul> </body> </html> Thursday, April 4, 13 -
var MyController = function($scope, $http) { $http.get('https://api.github.com/repos/ angular/angular.js/commits') .success(function(commits) {
$scope.commits = commits }) } Thursday, April 4, 13 -
var MyController = function($scope, $http) { $http.get('https://api.github.com/repos/ angular/angular.js/commits') .success(function(commits) {
$scope.commits = commits }) } Thursday, April 4, 13 -
var MyController = function($scope, $http) { $http.get('https://api.github.com/repos/ angular/angular.js/commits') .success(function(commits) {
$scope.commits = commits }) } Thursday, April 4, 13 -
var MyController = function($scope, $http) { $http.get('https://api.github.com/repos/ angular/angular.js/commits') .success(function(commits) {
$scope.commits = commits }) } Thursday, April 4, 13 -
var $httpBackend, scope, ctrl; beforeEach(inject(function($rootScope, _$httpBackend_, $controller) { $httpBackend =
_$httpBackend_; $httpBackend.expectGET('https://api.github.com/repos/angular/ angular.js/commits') .respond([ { "sha": "3a81dd" }, { "sha": "df9bff" } ]); scope = $rootScope.$new(); ctrl = $controller(MyController, {$scope: scope}); })); it('should get 2 commits from mock ajax', function() { $httpBackend.flush(); expect(scope.commits).toEqualData( [{ "sha": "3a81dd" }, { "sha": "df9bff" }]); }); Thursday, April 4, 13 -
var $httpBackend, scope, ctrl; beforeEach(inject(function($rootScope, _$httpBackend_, $controller) { $httpBackend =
_$httpBackend_; $httpBackend.expectGET('https://api.github.com/repos/angular/ angular.js/commits') .respond([ { "sha": "3a81dd" }, { "sha": "df9bff" } ]); scope = $rootScope.$new(); ctrl = $controller(MyController, {$scope: scope}); })); it('should get 2 commits from mock ajax', function() { $httpBackend.flush(); expect(scope.commits).toEqualData( [{ "sha": "3a81dd" }, { "sha": "df9bff" }]); }); Thursday, April 4, 13 -
var $httpBackend, scope, ctrl; beforeEach(inject(function($rootScope, _$httpBackend_, $controller) { $httpBackend =
_$httpBackend_; $httpBackend.expectGET('https://api.github.com/repos/angular/ angular.js/commits') .respond([ { "sha": "3a81dd" }, { "sha": "df9bff" } ]); scope = $rootScope.$new(); ctrl = $controller(MyController, {$scope: scope}); })); it('should get 2 commits from mock ajax', function() { $httpBackend.flush(); expect(scope.commits).toEqualData( [{ "sha": "3a81dd" }, { "sha": "df9bff" }]); }); Thursday, April 4, 13 -
var $httpBackend, scope, ctrl; beforeEach(inject(function($rootScope, _$httpBackend_, $controller) { $httpBackend =
_$httpBackend_; $httpBackend.expectGET('https://api.github.com/repos/angular/ angular.js/commits') .respond([ { "sha": "3a81dd" }, { "sha": "df9bff" } ]); scope = $rootScope.$new(); ctrl = $controller(MyController, {$scope: scope}); })); it('should get 2 commits from mock ajax', function() { $httpBackend.flush(); expect(scope.commits).toEqualData( [{ "sha": "3a81dd" }, { "sha": "df9bff" }]); }); Thursday, April 4, 13 -
var $httpBackend, scope, ctrl; beforeEach(inject(function($rootScope, _$httpBackend_, $controller) { $httpBackend =
_$httpBackend_; $httpBackend.expectGET('https://api.github.com/repos/angular/ angular.js/commits') .respond([ { "sha": "3a81dd" }, { "sha": "df9bff" } ]); scope = $rootScope.$new(); ctrl = $controller(MyController, {$scope: scope}); })); it('should get 2 commits from mock ajax', function() { $httpBackend.flush(); expect(scope.commits).toEqualData( [{ "sha": "3a81dd" }, { "sha": "df9bff" }]); }); Thursday, April 4, 13 -
var $httpBackend, scope, ctrl; beforeEach(inject(function($rootScope, _$httpBackend_, $controller) { $httpBackend =
_$httpBackend_; $httpBackend.expectGET('https://api.github.com/repos/angular/ angular.js/commits') .respond([ { "sha": "3a81dd" }, { "sha": "df9bff" } ]); scope = $rootScope.$new(); ctrl = $controller(MyController, {$scope: scope}); })); it('should get 2 commits from mock ajax', function() { $httpBackend.flush(); expect(scope.commits).toEqualData( [{ "sha": "3a81dd" }, { "sha": "df9bff" }]); }); Thursday, April 4, 13 -
var $httpBackend, scope, ctrl; beforeEach(inject(function($rootScope, _$httpBackend_, $controller) { $httpBackend =
_$httpBackend_; $httpBackend.expectGET('https://api.github.com/repos/angular/ angular.js/commits') .respond([ { "sha": "3a81dd" }, { "sha": "df9bff" } ]); scope = $rootScope.$new(); ctrl = $controller(MyController, {$scope: scope}); })); it('should get 2 commits from mock ajax', function() { $httpBackend.flush(); expect(scope.commits).toEqualData( [{ "sha": "3a81dd" }, { "sha": "df9bff" }]); }); Thursday, April 4, 13 -
var $httpBackend, scope, ctrl; beforeEach(inject(function($rootScope, _$httpBackend_, $controller) { $httpBackend =
_$httpBackend_; $httpBackend.expectGET('https://api.github.com/repos/angular/ angular.js/commits') .respond([ { "sha": "3a81dd" }, { "sha": "df9bff" } ]); scope = $rootScope.$new(); ctrl = $controller(MyController, {$scope: scope}); })); it('should get 2 commits from mock ajax', function() { $httpBackend.flush(); expect(scope.commits).toEqualData( [{ "sha": "3a81dd" }, { "sha": "df9bff" }]); }); Thursday, April 4, 13 -
How Angular loads your controller 1. Figure out what MyController
depends on $scope $http Thursday, April 4, 13 -
How Angular loads your controller 2. Finds a service that
implements each one $scope => // the scope {} $http => // the http service Thursday, April 4, 13 -
How Angular loads your controller 3. Calls MyController with injected
dependencies MyController(the-scope, the-http-svc); Thursday, April 4, 13 -
var MyController = function($scope, $http) { $http.get('https://api.github.com/repos/ angular/angular.js/commits') .success(function(commits) {
$scope.commits = commits }) } Thursday, April 4, 13 -
var MyController = function($http, $scope) { $http.get('https://api.github.com/repos/ angular/angular.js/commits') .success(function(commits) {
$scope.commits = commits }) } Thursday, April 4, 13 -
var divide = function(numerator, denominator){ return numerator / denominator; }
Thursday, April 4, 13 -
var divide = function(numerator, denominator){ return numerator / denominator; }
Thursday, April 4, 13 -
var divide = function(denominator, numerator){ return numerator / denominator; }
Thursday, April 4, 13 -
var FN_ARGS = /^function\s*[^\(]*\(\s*([^\)]*)\)/m; var FN_ARG_SPLIT = /,/; var FN_ARG
= /^\s*(_?)(\S+?)\1\s*$/; var STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg; function annotate(fn) { var $inject, fnText, argDecl, last; $inject = []; fnText = fn.toString().replace(STRIP_COMMENTS, ''); argDecl = fnText.match(FN_ARGS); forEach(argDecl[1].split(FN_ARG_SPLIT), function(arg){ arg.replace(FN_ARG, function(all, underscore, name){ $inject.push(name); }); }); return $inject; } Angular’s annotate() Thursday, April 4, 13 -
var FN_ARGS = /^function\s*[^\(]*\(\s*([^\)]*)\)/m; var FN_ARG_SPLIT = /,/; var FN_ARG
= /^\s*(_?)(\S+?)\1\s*$/; var STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg; function annotate(fn) { var $inject, fnText, argDecl, last; $inject = []; fnText = fn.toString().replace(STRIP_COMMENTS, ''); argDecl = fnText.match(FN_ARGS); forEach(argDecl[1].split(FN_ARG_SPLIT), function(arg){ arg.replace(FN_ARG, function(all, underscore, name){ $inject.push(name); }); }); return $inject; } Thursday, April 4, 13 -
var FN_ARGS = /^function\s*[^\(]*\(\s*([^\)]*)\)/m; var FN_ARG_SPLIT = /,/; var FN_ARG
= /^\s*(_?)(\S+?)\1\s*$/; var STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg; function annotate(fn) { var $inject, fnText, argDecl, last; $inject = []; fnText = fn.toString().replace(STRIP_COMMENTS, ''); argDecl = fnText.match(FN_ARGS); forEach(argDecl[1].split(FN_ARG_SPLIT), function(arg){ arg.replace(FN_ARG, function(all, underscore, name){ $inject.push(name); }); }); return $inject; } Thursday, April 4, 13 -
var FN_ARGS = /^function\s*[^\(]*\(\s*([^\)]*)\)/m; var FN_ARG_SPLIT = /,/; var FN_ARG
= /^\s*(_?)(\S+?)\1\s*$/; var STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg; function annotate(fn) { var $inject, fnText, argDecl, last; $inject = []; fnText = fn.toString().replace(STRIP_COMMENTS, ''); argDecl = fnText.match(FN_ARGS); forEach(argDecl[1].split(FN_ARG_SPLIT), function(arg){ arg.replace(FN_ARG, function(all, underscore, name){ $inject.push(name); }); }); return $inject; } Thursday, April 4, 13 -
How Angular loads your controller 1 Uses “annotate” what your
parameters are called [”$scope”, “$http”] or [“$http”, ”$scope”] Thursday, April 4, 13 -
How Angular loads your controller 1 Uses “annotate” what your
parameters are called 2. Finds a service that implements each one 3. Calls MyController with injected dependencies Thursday, April 4, 13 -
var MyController = function($scope, $http) { $http.get("https://api.github.com/repos/ angular/angular.js/commits") .success(function(commits) {
$scope.commits = commits }) } Thursday, April 4, 13 -
function annotate(fn) { var $inject, fnText, argDecl, last; if (typeof
fn == 'function') { // all the regular expression code } else if (isArray(fn)) { last = fn.length - 1; assertArgFn(fn[last], 'fn') $inject = fn.slice(0, last); } else { assertArgFn(fn, 'fn', true); } return $inject; } Thursday, April 4, 13 -
function annotate(fn) { var $inject, fnText, argDecl, last; if (typeof
fn == 'function') { // all the regular expression code } else if (isArray(fn)) { last = fn.length - 1; assertArgFn(fn[last], 'fn') $inject = fn.slice(0, last); } else { assertArgFn(fn, 'fn', true); } return $inject; } Thursday, April 4, 13 -
function annotate(fn) { var $inject, fnText, argDecl, last; if (typeof
fn == 'function') { // all the regular expression code } else if (isArray(fn)) { last = fn.length - 1; assertArgFn(fn[last], 'fn') $inject = fn.slice(0, last); } else { assertArgFn(fn, 'fn', true); } return $inject; } Thursday, April 4, 13 -
function annotate(fn) { var $inject, fnText, argDecl, last; if (typeof
fn == 'function') { // all the regular expression code } else if (isArray(fn)) { last = fn.length - 1; assertArgFn(fn[last], 'fn') $inject = fn.slice(0, last); } else { assertArgFn(fn, 'fn', true); } return $inject; } Thursday, April 4, 13 -
function annotate(fn) { var $inject, fnText, argDecl, last; if (typeof
fn == 'function') { // all the regular expression code } else if (isArray(fn)) { last = fn.length - 1; assertArgFn(fn[last], 'fn') $inject = fn.slice(0, last); } else { assertArgFn(fn, 'fn', true); } return $inject; } Thursday, April 4, 13 -
var MyController = ['$scope', '$http', function(e,t) { t.get("https://api.github.com/repos/angular/ angular.js/commits") .success(function(t)
{ e.commits=t }) }] Thursday, April 4, 13 -
Use the array syntax var MyController = ['$scope', '$http', function($scope,$http)
{ ... } ] Thursday, April 4, 13 -
The array syntax is repetitive Try the Angular Minifier https://github.com/btford/ngmin
Thursday, April 4, 13 -
Thank You Photos from http://blackandwtf.tumblr.com Alex Rothenberg http://www.alexrothenberg.com @alexrothenberg https://github.com/alexrothenberg
McKinsey & Co. Thursday, April 4, 13