Modules and namespace / name collision in AngularJS

前端 未结 4 1895
故里飘歌
故里飘歌 2020-11-30 05:41

Consider the following jfiddle http://jsfiddle.net/bchapman26/9uUBU/29/

//angular.js example for factory vs service
var app = angular.module(\'myApp\', [\'mo         


        
4条回答
  •  有刺的猬
    2020-11-30 05:50

    You can avoid this situation by using a convention to name your modules so that they always unique.

    One approach is to look at how other languages do it. For example in Java the “full name” of the class is based on the name of the file and the folder it’s in. For example if you had a Java file called Bitmap.java in the folder MyArtStuff, the full name of the class would be MyArtStuff.Bitmap

    Turns out AngularJS allows you to have dots (.) as part of your module name so you could essentially use the name convention.

    For example if a developer create a module called “ModuleA” in the script “MainPage\Module1.js” they should name their module “MainPage.Module1.ModuleA”. Because each path and filename is unique in your app then your module name will be unique.

    You would just have to get your developers to follow this convention.

    Note as Rockallite points out this will not help with services, controllers, etc having the same name in multiple modules. But you can use a similiar approach to result that and prefix the names of those elements as well.

    Ideally AngularJS would have namespaces and in the future it might. Until then the best we can do is do what developers have been doings for over 40 years before namespaces were invented and prefix our elements best we can.

提交回复
热议问题