From 80341cb9badd952fdc80094df4123629313b4cc4 Mon Sep 17 00:00:00 2001 From: Misko Hevery Date: Wed, 1 May 2013 20:55:25 -0400 Subject: [PATCH] feat(injector): add has method for querying Closes #2556 --- src/auto/injector.js | 5 ++++- src/ng/animation.js | 10 +++------- test/auto/injectorSpec.js | 9 +++++++++ 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/auto/injector.js b/src/auto/injector.js index b2795693634c..6b2865efa6c0 100644 --- a/src/auto/injector.js +++ b/src/auto/injector.js @@ -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); + } }; } } diff --git a/src/ng/animation.js b/src/ng/animation.js index 9a5a5a95882a..76cf943efb94 100644 --- a/src/ng/animation.js +++ b/src/ng/animation.js @@ -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); } } } diff --git a/test/auto/injectorSpec.js b/test/auto/injectorSpec.js index 6f84897bc900..8fd03be48cbb 100644 --- a/test/auto/injectorSpec.js +++ b/test/auto/injectorSpec.js @@ -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');