Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Commit

Permalink
feat(injector): add has method for querying
Browse files Browse the repository at this point in the history
Closes #2556
  • Loading branch information
mhevery committed May 2, 2013
1 parent 9956bae commit 80341cb
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
5 changes: 4 additions & 1 deletion src/auto/injector.js
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,10 @@ function createInjector(modulesToLoad) {
invoke: invoke,
instantiate: instantiate,
get: getService,
annotate: annotate
annotate: annotate,
has: function(name) {
return providerCache.hasOwnProperty(name + providerSuffix) || cache.hasOwnProperty(name);
}
};
}
}
10 changes: 3 additions & 7 deletions src/ng/animation.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,9 @@ function $AnimationProvider($provide) {
*/
return function $animation(name) {
if (name) {
try {
return $injector.get(camelCase(name) + suffix);
} catch (e) {
//TODO(misko): this is a hack! we should have a better way to test if the injector has a given key.
// The issue is that the animations are optional, and if not present they should be silently ignored.
// The proper way to fix this is to add API onto the injector so that we can ask to see if a given
// animation is supported.
var animationName = camelCase(name) + suffix;
if ($injector.has(animationName)) {
return $injector.get(animationName);
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions test/auto/injectorSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ describe('injector', function() {
});


it('should allow query names', function() {
providers('abc', function () { return ''; });

expect(injector.has('abc')).toBe(true);
expect(injector.has('xyz')).toBe(false);
expect(injector.has('$injector')).toBe(true);
});


it('should provide useful message if no provider', function() {
expect(function() {
injector.get('idontexist');
Expand Down

0 comments on commit 80341cb

Please sign in to comment.