angular / angular.js

AngularJS - HTML enhanced for web apps!
https://angularjs.org
MIT License
58.81k stars 27.49k forks source link

$provide.alias for aliasing (or renaming...?) injectables #12457

Closed matthewjh closed 8 years ago

matthewjh commented 9 years ago

Hi,

It's a minor issue, but there's an injectable from a third-party module that I need to use but I don't like how it's named (they decided to ClassCase the name although it's not a constructor, which disagrees with my naming conventions). In this scenario, among others, e.g. a naming conflict, it'd be helpful to be able to intuitively rename or alias injectables during the config stage.

1) Is there any existing means within angular to achieve this? 2) Would $provide.rename and/or $provide.alias methods be a good solution to this?

$provide.rename('newName', 'oldName')

pkozlowski-opensource commented 9 years ago

Wouldn't this work?

.factory('newName', function(OldName) {
   return OldName;
});

We could add a sugar for this of course, but given how people are freaking out about already existing sugar (value, service etc.)...

matthewjh commented 9 years ago

Hmm yes, that would be equivalent to alias. It's still not smack-in-the-face obvious what it's doing at a glance though, if you're scrolling through a lot of code.

What led me to this was angular 2's bind(..).toAlias(..), which reads so nicely.

oldName would still be on the injector though, so that couldn't be used for naming collisions (.rename's use case).

Note: I'm not sure whether rename would actually be implementable.

pkozlowski-opensource commented 9 years ago

@matthewjh we can't simply rename, IMO, since authors of the original module would be injecting using the old names. So solving the name collision / renaming pb is a different story and would be more complex as compared to simple aliasing

matthewjh commented 9 years ago

Yeah, that makes sense.

petebacondarwin commented 8 years ago

I feel that the workaround for adding an alias that @pkozlowski-opensource suggested is good enough. It is very short and if it is not clear then a simple comment would suffice. As already agreed we would not be able to do a rename.