feat(injector): add has method for querying

Closes #2556
This commit is contained in:
Misko Hevery
2013-05-01 20:55:25 -04:00
parent 9956baedd7
commit 80341cb9ba
3 changed files with 16 additions and 8 deletions

View File

@@ -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);
}
};
}
}

View File

@@ -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);
}
}
}

View File

@@ -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');