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

Commit 80341cb

Browse files
committed
feat(injector): add has method for querying
Closes #2556
1 parent 9956bae commit 80341cb

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

src/auto/injector.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,10 @@ function createInjector(modulesToLoad) {
597597
invoke: invoke,
598598
instantiate: instantiate,
599599
get: getService,
600-
annotate: annotate
600+
annotate: annotate,
601+
has: function(name) {
602+
return providerCache.hasOwnProperty(name + providerSuffix) || cache.hasOwnProperty(name);
603+
}
601604
};
602605
}
603606
}

src/ng/animation.js

+3-7
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,9 @@ function $AnimationProvider($provide) {
5151
*/
5252
return function $animation(name) {
5353
if (name) {
54-
try {
55-
return $injector.get(camelCase(name) + suffix);
56-
} catch (e) {
57-
//TODO(misko): this is a hack! we should have a better way to test if the injector has a given key.
58-
// The issue is that the animations are optional, and if not present they should be silently ignored.
59-
// The proper way to fix this is to add API onto the injector so that we can ask to see if a given
60-
// animation is supported.
54+
var animationName = camelCase(name) + suffix;
55+
if ($injector.has(animationName)) {
56+
return $injector.get(animationName);
6157
}
6258
}
6359
}

test/auto/injectorSpec.js

+9
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,15 @@ describe('injector', function() {
5858
});
5959

6060

61+
it('should allow query names', function() {
62+
providers('abc', function () { return ''; });
63+
64+
expect(injector.has('abc')).toBe(true);
65+
expect(injector.has('xyz')).toBe(false);
66+
expect(injector.has('$injector')).toBe(true);
67+
});
68+
69+
6170
it('should provide useful message if no provider', function() {
6271
expect(function() {
6372
injector.get('idontexist');

0 commit comments

Comments
 (0)