refactor(injector): $injector is no longer a function.

- $injector('abc') -> $injector.get('abc');
- $injector(fn) -> $injector.invoke(null, fn);
This commit is contained in:
Misko Hevery 2011-11-12 15:23:30 -08:00
parent 8d6dc0b9a7
commit e88dfb734a
16 changed files with 182 additions and 179 deletions

View file

@ -3,8 +3,8 @@ describe('example.personalLog.LogCtrl', function() {
function createNotesCtrl() { function createNotesCtrl() {
var injector = angular.injector('ng', 'ngMock'); var injector = angular.injector('ng', 'ngMock');
var scope = injector('$rootScope'); var scope = injector.get('$rootScope');
scope.$cookies = injector('$cookies'); scope.$cookies = injector.get('$cookies');
return scope.$new(example.personalLog.LogCtrl); return scope.$new(example.personalLog.LogCtrl);
} }

View file

@ -849,12 +849,14 @@ function angularInit(config, document){
function bootstrap(element, modules) { function bootstrap(element, modules) {
modules = modules || []; modules = modules || [];
modules.unshift(ngModule); modules.unshift(ngModule);
createInjector(modules, angularModule)(['$rootScope', '$compile', '$injector', function(scope, compile, injector){ createInjector(modules, angularModule).invoke(null,
scope.$apply(function() { ['$rootScope', '$compile', '$injector', function(scope, compile, injector){
element.data('$injector', injector); scope.$apply(function() {
compile(element)(scope); element.data('$injector', injector);
}); compile(element)(scope);
}]); });
}]
);
} }
function angularJsConfig(document) { function angularJsConfig(document) {

View file

@ -22,7 +22,7 @@
* *
* // use the injector to kick of your application * // use the injector to kick of your application
* // use the type inference to auto inject arguments, or use implicit injection * // use the type inference to auto inject arguments, or use implicit injection
* $injector(function($rootScope, $compile, $document){ * $injector.invoke(null, function($rootScope, $compile, $document){
* $compile($document)($rootScope); * $compile($document)($rootScope);
* $rootScope.$digest(); * $rootScope.$digest();
* }); * });
@ -60,21 +60,22 @@ function inferInjectionArgs(fn) {
/////////////////////////////////////// ///////////////////////////////////////
/** /**
* @ngdoc function * @ngdoc object
* @name angular.module.AUTO.$injector * @name angular.module.AUTO.$injector
* @function * @function
* *
* @description * @description
* *
* `$injector` function is used to retrieve object instances. Object instances are defined by * `$injector` is used to retrieve object instances as defined by
* {@link angular.module.AUTO.$provide provider}. * {@link angular.module.AUTO.$provide provider}, instantiate types, invoke methods,
* and load modules.
* *
* The following always holds true: * The following always holds true:
* *
* <pre> * <pre>
* var $injector = angular.injector(); * var $injector = angular.injector();
* expect($injector('$injector')).toBe($injector); * expect($injector.get('$injector')).toBe($injector);
* expect($injector(function($injector){ * expect($injector.invoke(null, function($injector){
* return $injector; * return $injector;
* }).toBe($injector); * }).toBe($injector);
* </pre> * </pre>
@ -108,14 +109,6 @@ function inferInjectionArgs(fn) {
* *
* ## Inline * ## Inline
* As an array of injection names, where the last item in the array is the function to call. * As an array of injection names, where the last item in the array is the function to call.
*
* @param {string, function()} argument If the `argument` is:
*
* - `string`: Retrieve an instance of a named object. If object does not exist, use the provider to create
* a new instance. Calling the method repeatedly with the same name will always return the same
* instance.
* - `function`: Invoke the function. This is a short hand for `$injector.`{@link #invoke invoke(null, argument)}.
* @return the object instance or the return value of the invoked function.
*/ */
/** /**
@ -261,113 +254,116 @@ function inferInjectionArgs(fn) {
function createInjector(modulesToLoad, moduleRegistry) { function createInjector(modulesToLoad, moduleRegistry) {
var cache = {}, var cache = {},
$injector = internalInjector(cache),
providerSuffix = 'Provider', providerSuffix = 'Provider',
providerSuffixLength = providerSuffix.length; providerSuffixLength = providerSuffix.length,
path = [],
$injector;
value('$injector', $injector); value('$injector', $injector = {
value('$provide', {service: service, factory: factory, value: value}); get: getService,
invoke: invoke,
instantiate: instantiate,
loadModule: loadModule
});
value('$provide', {
service: service,
factory: factory,
value: value
});
loadModule(modulesToLoad);
return $injector;
////////////////////////////////////
function service(name, provider) { function service(name, provider) {
if (isFunction(provider)){ if (isFunction(provider)){
provider = $injector.instantiate(provider); provider = instantiate(provider);
} }
if (!provider.$get) { if (!provider.$get) {
throw Error('Providers must define $get factory method.'); throw Error('Providers must define $get factory method.');
} }
cache['#' + name + providerSuffix] = provider; cache['#' + name + providerSuffix] = provider;
}; }
function factory(name, factoryFn) { service(name, { $get:factoryFn }); };
function value(name, value) { factory(name, valueFn(value)); };
function internalInjector(cache) { function factory(name, factoryFn) { service(name, { $get:factoryFn }); }
var path = [];
function injector(value) { function value(name, value) { factory(name, valueFn(value)); }
switch(typeof value) {
case 'function': function getService(value) {
return invoke(null, value); if (typeof value !== 'string') {
case 'string': throw Error('Service name expected');
var instanceKey = '#' + value, }
instance = cache[instanceKey]; var instanceKey = '#' + value,
if (instance !== undefined || cache.hasOwnProperty(instanceKey)) { instance = cache[instanceKey];
return instance; if (instance !== undefined || cache.hasOwnProperty(instanceKey)) {
} return instance;
try { }
path.unshift(value); try {
var providerKey = instanceKey + providerSuffix, path.unshift(value);
provider = cache[providerKey]; var providerKey = instanceKey + providerSuffix,
if (provider) { provider = cache[providerKey];
return cache[instanceKey] = invoke(provider, provider.$get); if (provider) {
} else { return cache[instanceKey] = invoke(provider, provider.$get);
throw Error("Unknown provider for '" + path.join("' <- '") + "'."); } else {
} throw Error("Unknown provider for '" + path.join("' <- '") + "'.");
} finally {
path.shift();
}
case 'object':
if (isArray(value)) {
return invoke(null, value);
}
default:
throw Error('Injector expects name or function.');
} }
} finally {
path.shift();
}
}
function invoke(self, fn, locals){
var args = [],
$inject,
length,
key;
if (typeof fn == 'function') {
$inject = inferInjectionArgs(fn);
length = $inject.length;
} else {
if (isArray(fn)) {
$inject = fn;
length = $inject.length;
fn = $inject[--length];
}
assertArgFn(fn, 'fn');
} }
function invoke(self, fn, locals){ while(length--) {
var args = [], key = $inject[length];
$inject, args.unshift(
locals && locals.hasOwnProperty(key)
? locals[key]
: getService($inject[length], path)
);
}
length, // Performance optimization: http://jsperf.com/apply-vs-call-vs-invoke
key; switch (self ? -1 : args.length) {
case 0: return fn();
case 1: return fn(args[0]);
case 2: return fn(args[0], args[1]);
case 3: return fn(args[0], args[1], args[2]);
case 4: return fn(args[0], args[1], args[2], args[3]);
case 5: return fn(args[0], args[1], args[2], args[3], args[4]);
case 6: return fn(args[0], args[1], args[2], args[3], args[4], args[5]);
case 7: return fn(args[0], args[1], args[2], args[3], args[4], args[5], args[6]);
case 8: return fn(args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7]);
case 9: return fn(args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8]);
case 10: return fn(args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8], args[9]);
default: return fn.apply(self, args);
}
}
if (typeof fn == 'function') { function instantiate(Type, locals){
$inject = inferInjectionArgs(fn); var Constructor = function(){},
length = $inject.length; instance;
} else { Constructor.prototype = Type.prototype;
if (isArray(fn)) { instance = new Constructor();
$inject = fn; return invoke(instance, Type, locals) || instance;
length = $inject.length;
fn = $inject[--length];
}
assertArgFn(fn, 'fn');
}
while(length--) {
key = $inject[length];
args.unshift(
locals && locals.hasOwnProperty(key)
? locals[key]
: injector($inject[length], path)
);
}
switch (self ? -1 : args.length) {
case 0: return fn();
case 1: return fn(args[0]);
case 2: return fn(args[0], args[1]);
case 3: return fn(args[0], args[1], args[2]);
case 4: return fn(args[0], args[1], args[2], args[3]);
case 5: return fn(args[0], args[1], args[2], args[3], args[4]);
case 6: return fn(args[0], args[1], args[2], args[3], args[4], args[5]);
case 7: return fn(args[0], args[1], args[2], args[3], args[4], args[5], args[6]);
case 8: return fn(args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7]);
case 9: return fn(args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8]);
case 10: return fn(args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8], args[9]);
default: return fn.apply(self, args);
}
};
injector.invoke = invoke;
injector.instantiate = function(Type, locals){
var Constructor = function(){},
instance;
Constructor.prototype = Type.prototype;
instance = new Constructor();
return invoke(instance, Type, locals) || instance;
};
injector.loadModule = loadModule;
return injector;
} }
function loadModule(modulesToLoad){ function loadModule(modulesToLoad){
@ -380,15 +376,10 @@ function createInjector(modulesToLoad, moduleRegistry) {
} }
} }
if (isFunction(module) || isArray(module)) { if (isFunction(module) || isArray(module)) {
$injector(module); invoke(null, module);
} else { } else {
assertArgFn(module, 'module'); assertArgFn(module, 'module');
} }
}); });
} }
loadModule(modulesToLoad);
return $injector;
} }

View file

@ -783,7 +783,7 @@ window.jasmine && (function(window){
return function(){ return function(){
var injector = this.$injector; var injector = this.$injector;
if (!injector) { if (!injector) {
injector = this.$injector = angular.injector('ng', 'ngMock'); injector = this.$injector = angular.injector('ng', 'ngMock');
} }
for(var i = 0, ii = blockFns.length; i < ii; i++) { for(var i = 0, ii = blockFns.length; i < ii; i++) {
injector.invoke(this, blockFns[i]); injector.invoke(this, blockFns[i]);

View file

@ -92,7 +92,7 @@ angular.scenario.Application.prototype.executeAction = function(action) {
} }
var element = $window.angular.element($window.document); var element = $window.angular.element($window.document);
var $injector = element.inheritedData('$injector'); var $injector = element.inheritedData('$injector');
$injector(function($browser){ $injector.invoke(null, function($browser){
$browser.notifyWhenNoOutstandingRequests(function() { $browser.notifyWhenNoOutstandingRequests(function() {
action.call(self, $window, _jQuery($window.document)); action.call(self, $window, _jQuery($window.document));
}); });

View file

@ -163,7 +163,7 @@ angular.scenario.Runner.prototype.createSpecRunner_ = function(scope) {
*/ */
angular.scenario.Runner.prototype.run = function(application) { angular.scenario.Runner.prototype.run = function(application) {
var self = this; var self = this;
var $root = angular.injector('ng')('$rootScope'); var $root = angular.injector('ng').get('$rootScope');
angular.extend($root, this); angular.extend($root, this);
angular.forEach(angular.scenario.Runner.prototype, function(fn, name) { angular.forEach(angular.scenario.Runner.prototype, function(fn, name) {
$root[name] = angular.bind(self, fn); $root[name] = angular.bind(self, fn);

View file

@ -103,25 +103,25 @@ angular.scenario.dsl('browser', function() {
api.url = function() { api.url = function() {
return this.addFutureAction('$location.url()', function($window, $document, done) { return this.addFutureAction('$location.url()', function($window, $document, done) {
done(null, $window.angular.injector('ng')('$location').url()); done(null, $window.angular.injector('ng').get('$location').url());
}); });
}; };
api.path = function() { api.path = function() {
return this.addFutureAction('$location.path()', function($window, $document, done) { return this.addFutureAction('$location.path()', function($window, $document, done) {
done(null, $window.angular.injector('ng')('$location').path()); done(null, $window.angular.injector('ng').get('$location').path());
}); });
}; };
api.search = function() { api.search = function() {
return this.addFutureAction('$location.search()', function($window, $document, done) { return this.addFutureAction('$location.search()', function($window, $document, done) {
done(null, $window.angular.injector('ng')('$location').search()); done(null, $window.angular.injector('ng').get('$location').search());
}); });
}; };
api.hash = function() { api.hash = function() {
return this.addFutureAction('$location.hash()', function($window, $document, done) { return this.addFutureAction('$location.hash()', function($window, $document, done) {
done(null, $window.angular.injector('ng')('$location').hash()); done(null, $window.angular.injector('ng').get('$location').hash());
}); });
}; };

View file

@ -143,7 +143,8 @@ function $CompileProvider(){
* - If you are not asking the linking function to clone the template, create the DOM element(s) * - If you are not asking the linking function to clone the template, create the DOM element(s)
* before you send them to the compiler and keep this reference around. * before you send them to the compiler and keep this reference around.
* <pre> * <pre>
* var scope = angular.injector('ng')(function($rootScope, $compile){ * var $injector = angular.injector('ng');
* var scope = $injector.invoke(null, function($rootScope, $compile){
* var element = $compile('<p>{{total}}</p>')($rootScope); * var element = $compile('<p>{{total}}</p>')($rootScope);
* }); * });
* </pre> * </pre>

View file

@ -85,7 +85,7 @@ function $FilterProvider($provide) {
this.$get = ['$injector', function($injector) { this.$get = ['$injector', function($injector) {
return function(name) { return function(name) {
return $injector(name + suffix); return $injector.get(name + suffix);
} }
}]; }];

View file

@ -386,7 +386,7 @@ describe('angular', function() {
expect(angular.injector(function($provide){ expect(angular.injector(function($provide){
$provide.factory('svc1', function() { return 'svc1'; }); $provide.factory('svc1', function() { return 'svc1'; });
$provide.factory('svc2', ['svc1', function(s) { return 'svc2-' + s; }]); $provide.factory('svc2', ['svc1', function(s) { return 'svc2-' + s; }]);
})('svc2')).toEqual('svc2-svc1'); }).get('svc2')).toEqual('svc2-svc1');
}); });
}); });

View file

@ -15,16 +15,16 @@ describe('injector', function() {
var instance = {}, var instance = {},
original = instance; original = instance;
providers('instance', function() { return instance; }); providers('instance', function() { return instance; });
expect(injector('instance')).toEqual(instance); expect(injector.get('instance')).toEqual(instance);
instance = 'deleted'; instance = 'deleted';
expect(injector('instance')).toEqual(original); expect(injector.get('instance')).toEqual(original);
}); });
it('should inject providers', function() { it('should inject providers', function() {
providers('a', function() {return 'Mi';}); providers('a', function() {return 'Mi';});
providers('b', function(mi) {return mi+'sko';}, {$inject:['a']}); providers('b', function(mi) {return mi+'sko';}, {$inject:['a']});
expect(injector('b')).toEqual('Misko'); expect(injector.get('b')).toEqual('Misko');
}); });
@ -47,7 +47,7 @@ describe('injector', function() {
providers('s5', function() { log.push('s5'); }); providers('s5', function() { log.push('s5'); });
providers('s6', function() { log.push('s6'); }); providers('s6', function() { log.push('s6'); });
injector('s1'); injector.get('s1');
expect(log).toEqual(['s6', 's5', 's3', 's4', 's2', 's1']); expect(log).toEqual(['s6', 's5', 's3', 's4', 's2', 's1']);
}); });
@ -55,7 +55,7 @@ describe('injector', function() {
it('should provide useful message if no provider', function() { it('should provide useful message if no provider', function() {
expect(function() { expect(function() {
injector('idontexist'); injector.get('idontexist');
}).toThrow("Unknown provider for 'idontexist'."); }).toThrow("Unknown provider for 'idontexist'.");
}); });
@ -63,7 +63,7 @@ describe('injector', function() {
providers('a', function(idontexist) {return 1;}); providers('a', function(idontexist) {return 1;});
providers('b', function(a) {return 2;}); providers('b', function(a) {return 2;});
expect(function() { expect(function() {
injector('b'); injector.get('b');
}).toThrow("Unknown provider for 'idontexist' <- 'a' <- 'b'."); }).toThrow("Unknown provider for 'idontexist' <- 'a' <- 'b'.");
}); });
@ -100,7 +100,7 @@ describe('injector', function() {
it('should invoke the passed in function with all of the dependencies as arguments', function(){ it('should invoke the passed in function with all of the dependencies as arguments', function(){
providers('c', function() {return 3;}); providers('c', function() {return 3;});
providers('d', function() {return 4;}); providers('d', function() {return 4;});
expect(injector(['a', 'b', 'c', 'd', fn])).toEqual(10); expect(injector.invoke(null, ['a', 'b', 'c', 'd', fn])).toEqual(10);
}); });
@ -163,7 +163,7 @@ describe('injector', function() {
it('should have $injector', function() { it('should have $injector', function() {
var $injector = createInjector(); var $injector = createInjector();
expect($injector('$injector')).toBe($injector); expect($injector.get('$injector')).toBe($injector);
}); });
it('should define module', function() { it('should define module', function() {
@ -176,7 +176,7 @@ describe('injector', function() {
}); });
}, function(valueProvider, fnProvider, serviceProvider) { }, function(valueProvider, fnProvider, serviceProvider) {
log += valueProvider.$get() + fnProvider.$get() + serviceProvider.$get(); log += valueProvider.$get() + fnProvider.$get() + serviceProvider.$get();
}])(function(value, fn, service) { }]).invoke(null, function(value, fn, service) {
log += '->' + value + fn + service; log += '->' + value + fn + service;
}); });
expect(log).toEqual('value;function;service;->value;function;service;'); expect(log).toEqual('value;function;service;->value;function;service;');
@ -189,8 +189,8 @@ describe('injector', function() {
$injector = createInjector([ $injector = createInjector([
angular.extend(function(p) { $provide = p; }, {$inject: ['$provide']}) angular.extend(function(p) { $provide = p; }, {$inject: ['$provide']})
]); ]);
expect($injector('$injector')).toBe($injector); expect($injector.get('$injector')).toBe($injector);
expect($injector('$provide')).toBe($provide); expect($injector.get('$provide')).toBe($provide);
}); });
@ -210,9 +210,9 @@ describe('injector', function() {
p.value('c', serviceB.$get() + 'C'); p.value('c', serviceB.$get() + 'C');
}] }]
]); ]);
expect($injector('a')).toEqual('A'); expect($injector.get('a')).toEqual('A');
expect($injector('b')).toEqual('AB'); expect($injector.get('b')).toEqual('AB');
expect($injector('c')).toEqual('ABC'); expect($injector.get('c')).toEqual('ABC');
}); });
@ -222,7 +222,7 @@ describe('injector', function() {
provide.value('a', 'abc'); provide.value('a', 'abc');
}] }]
}); });
expect($injector('a')).toEqual('abc'); expect($injector.get('a')).toEqual('abc');
}); });
it('should error on invalid madule name', function(){ it('should error on invalid madule name', function(){
@ -237,7 +237,7 @@ describe('injector', function() {
it('should configure $provide values', function() { it('should configure $provide values', function() {
expect(createInjector([function($provide) { expect(createInjector([function($provide) {
$provide.value('value', 'abc'); $provide.value('value', 'abc');
}])('value')).toEqual('abc'); }]).get('value')).toEqual('abc');
}); });
}); });
@ -246,7 +246,7 @@ describe('injector', function() {
it('should configure $provide factory function', function() { it('should configure $provide factory function', function() {
expect(createInjector([function($provide) { expect(createInjector([function($provide) {
$provide.factory('value', valueFn('abc')); $provide.factory('value', valueFn('abc'));
}])('value')).toEqual('abc'); }]).get('value')).toEqual('abc');
}); });
}); });
@ -257,7 +257,7 @@ describe('injector', function() {
$provide.service('value', { $provide.service('value', {
$get: valueFn('abc') $get: valueFn('abc')
}); });
}])('value')).toEqual('abc'); }]).get('value')).toEqual('abc');
}); });
@ -269,7 +269,7 @@ describe('injector', function() {
}; };
expect(createInjector([function($provide) { expect(createInjector([function($provide) {
$provide.service('value', Type); $provide.service('value', Type);
}])('value')).toEqual('abc'); }]).get('value')).toEqual('abc');
}); });
}); });
}); });
@ -318,14 +318,14 @@ describe('injector', function() {
it('should retrieve by name and cache instance', function() { it('should retrieve by name and cache instance', function() {
expect(instance).toEqual({name: 'angular'}); expect(instance).toEqual({name: 'angular'});
expect($injector('instance')).toBe(instance); expect($injector.get('instance')).toBe(instance);
expect($injector('instance')).toBe(instance); expect($injector.get('instance')).toBe(instance);
}); });
it('should call functions and infer arguments', function() { it('should call functions and infer arguments', function() {
expect($injector(function(instance) { return instance; })).toBe(instance); expect($injector.invoke(null, function(instance) { return instance; })).toBe(instance);
expect($injector(function(instance) { return instance; })).toBe(instance); expect($injector.invoke(null, function(instance) { return instance; })).toBe(instance);
}); });
}); });
@ -342,7 +342,9 @@ describe('injector', function() {
it('should invoke method', function() { it('should invoke method', function() {
expect($injector(function(book, author) { return author + ':' + book;})).toEqual('melville:moby'); expect($injector.invoke(null, function(book, author) {
return author + ':' + book;
})).toEqual('melville:moby');
expect($injector.invoke($injector, function(book, author) { expect($injector.invoke($injector, function(book, author) {
expect(this).toEqual($injector); expect(this).toEqual($injector);
return author + ':' + book;})).toEqual('melville:moby'); return author + ':' + book;})).toEqual('melville:moby');
@ -350,7 +352,9 @@ describe('injector', function() {
it('should invoke method with locals', function() { it('should invoke method with locals', function() {
expect($injector(function(book, author) { return author + ':' + book;})).toEqual('melville:moby'); expect($injector.invoke(null, function(book, author) {
return author + ':' + book;
})).toEqual('melville:moby');
expect($injector.invoke($injector, expect($injector.invoke($injector,
function(book, author, chapter) { function(book, author, chapter) {
expect(this).toEqual($injector); expect(this).toEqual($injector);
@ -360,8 +364,9 @@ describe('injector', function() {
it('should invoke method which is annotated', function() { it('should invoke method which is annotated', function() {
expect($injector(extend(function(b, a) { return a + ':' + b}, {$inject:['book', 'author']}))). expect($injector.invoke(null, extend(function(b, a) {
toEqual('melville:moby'); return a + ':' + b
}, {$inject:['book', 'author']}))).toEqual('melville:moby');
expect($injector.invoke($injector, extend(function(b, a) { expect($injector.invoke($injector, extend(function(b, a) {
expect(this).toEqual($injector); expect(this).toEqual($injector);
return a + ':' + b; return a + ':' + b;
@ -370,7 +375,9 @@ describe('injector', function() {
it('should invoke method which is an array of annotation', function() { it('should invoke method which is an array of annotation', function() {
expect($injector(function(book, author) { return author + ':' + book;})).toEqual('melville:moby'); expect($injector.invoke(null, function(book, author) {
return author + ':' + book;
})).toEqual('melville:moby');
expect($injector.invoke($injector, function(book, author) { expect($injector.invoke($injector, function(book, author) {
expect(this).toEqual($injector); expect(this).toEqual($injector);
return author + ':' + book; return author + ':' + book;

View file

@ -150,7 +150,7 @@ describe("markups", function() {
it('should bind Text with no Bindings', inject(function($compile) { it('should bind Text with no Bindings', inject(function($compile) {
var $rootScope; var $rootScope;
function newScope (){ function newScope (){
return $rootScope = angular.injector('ng')('$rootScope'); return $rootScope = angular.injector('ng').get('$rootScope');
} }
forEach(['checked', 'disabled', 'multiple', 'readonly', 'selected'], function(name) { forEach(['checked', 'disabled', 'multiple', 'readonly', 'selected'], function(name) {
var element = $compile('<div ng:' + name + '="some"></div>')(newScope()) var element = $compile('<div ng:' + name + '="some"></div>')(newScope())

View file

@ -11,7 +11,7 @@ describe("angular.scenario.dsl", function() {
angular: new angular.scenario.testing.MockAngular() angular: new angular.scenario.testing.MockAngular()
}; };
$window.document.data('$injector', $injector); $window.document.data('$injector', $injector);
$root = $injector('$rootScope'); $root = $injector.get('$rootScope');
$root.emit = function(eventName) { $root.emit = function(eventName) {
eventLog.push(eventName); eventLog.push(eventName);
}; };
@ -162,16 +162,18 @@ describe("angular.scenario.dsl", function() {
describe('location', function() { describe('location', function() {
beforeEach(function() { beforeEach(function() {
$window.angular.injector = function() { $window.angular.injector = function() {
return function(serviceId) { return {
if (serviceId == '$location') { get: function(serviceId) {
return { if (serviceId == '$location') {
url: function() {return '/path?search=a#hhh';}, return {
path: function() {return '/path';}, url: function() {return '/path?search=a#hhh';},
search: function() {return {search: 'a'};}, path: function() {return '/path';},
hash: function() {return 'hhh';} search: function() {return {search: 'a'};},
}; hash: function() {return 'hhh';}
};
}
throw new Error('unknown service id ' + serviceId);
} }
throw new Error('unknown service id ' + serviceId);
}; };
}; };
}); });

View file

@ -285,7 +285,7 @@ describe('Scope', function() {
it('should return a function that allows listeners to be unregistered', inject(function($rootScope) { it('should return a function that allows listeners to be unregistered', inject(function($rootScope) {
var root = angular.injector('ng')('$rootScope'), var root = angular.injector('ng').get('$rootScope'),
listener = jasmine.createSpy('watch listener'), listener = jasmine.createSpy('watch listener'),
listenerRemove; listenerRemove;
@ -470,7 +470,7 @@ describe('Scope', function() {
it('should add listener for both $emit and $broadcast events', inject(function($rootScope) { it('should add listener for both $emit and $broadcast events', inject(function($rootScope) {
var log = '', var log = '',
root = angular.injector('ng')('$rootScope'), root = angular.injector('ng').get('$rootScope'),
child = root.$new(); child = root.$new();
function eventFn() { function eventFn() {
@ -490,7 +490,7 @@ describe('Scope', function() {
it('should return a function that deregisters the listener', inject(function($rootScope) { it('should return a function that deregisters the listener', inject(function($rootScope) {
var log = '', var log = '',
root = angular.injector('ng')('$rootScope'), root = angular.injector('ng').get('$rootScope'),
child = root.$new(), child = root.$new(),
listenerRemove; listenerRemove;
@ -669,7 +669,7 @@ describe('Scope', function() {
describe('listener', function() { describe('listener', function() {
it('should receive event object', inject(function($rootScope) { it('should receive event object', inject(function($rootScope) {
var scope = angular.injector('ng')('$rootScope'), var scope = angular.injector('ng').get('$rootScope'),
child = scope.$new(), child = scope.$new(),
event; event;
@ -685,7 +685,7 @@ describe('Scope', function() {
it('should support passing messages as varargs', inject(function($rootScope) { it('should support passing messages as varargs', inject(function($rootScope) {
var scope = angular.injector('ng')('$rootScope'), var scope = angular.injector('ng').get('$rootScope'),
child = scope.$new(), child = scope.$new(),
args; args;

View file

@ -31,8 +31,8 @@ beforeEach(function() {
afterEach(function() { afterEach(function() {
if (this.$injector) { if (this.$injector) {
var $rootScope = this.$injector('$rootScope'); var $rootScope = this.$injector.get('$rootScope');
var $log = this.$injector('$log'); var $log = this.$injector.get('$log');
// release the injector // release the injector
dealoc($rootScope); dealoc($rootScope);

View file

@ -507,15 +507,15 @@ describe("widget", function() {
it('should be possible to nest ng:view in ng:include', inject(function() { it('should be possible to nest ng:view in ng:include', inject(function() {
var injector = angular.injector('ng', 'ngMock'); var injector = angular.injector('ng', 'ngMock');
var myApp = injector('$rootScope'); var myApp = injector.get('$rootScope');
var $browser = injector('$browser'); var $browser = injector.get('$browser');
$browser.xhr.expectGET('includePartial.html').respond('view: <ng:view></ng:view>'); $browser.xhr.expectGET('includePartial.html').respond('view: <ng:view></ng:view>');
injector('$location').path('/foo'); injector.get('$location').path('/foo');
var $route = injector('$route'); var $route = injector.get('$route');
$route.when('/foo', {controller: angular.noop, template: 'viewPartial.html'}); $route.when('/foo', {controller: angular.noop, template: 'viewPartial.html'});
var element = injector('$compile')( var element = injector.get('$compile')(
'<div>' + '<div>' +
'include: <ng:include src="\'includePartial.html\'"> </ng:include>' + 'include: <ng:include src="\'includePartial.html\'"> </ng:include>' +
'</div>')(myApp); '</div>')(myApp);