javascript - Why 2 different Module can access each other when added as depend to third module? -
i having 3 modules in angularjs app, e.g. main
, home
, product
. main
module having home
, product
module dependencies (ng.module('main', ['home', 'product'])
) while home
, product
modules not having dependencies(ng.module('product', [])
ng.module('phome', [])
), still product
module can access home
module service? why???
below sample code of application, having same scenario , same issue. , jsfiddle link.
<!doctype html> <html ng-app="main"> <body ng-controller="maincontroller maincontroller"> {{maincontroller.name}} <script type="application/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script> <script> (function (ng) { var homemodule = ng.module('home', []); homemodule.service("homeservice", [function () { var homeservice = this; homeservice.getname = function () { return "home service"; } }]); var productmodule = ng.module('product', []); productmodule.service("productservice", ["homeservice", function (homeservice) { var productservice = this; productservice.getname = function () { return "product service - " + homeservice.getname(); }; }]); var mainmodule = ng.module('main', ['home', 'product']); mainmodule.controller("maincontroller", ['productservice', function (productservice) { var maincontroller = this; maincontroller.name = productservice.getname(); }]); })(angular); </script> </body> </html>
the answer pretty simple. angular doesn't scope contents of module module itself. i've read somewhere there have been discussions of adding functionality, haven't yet seen implemented.
to make matters worse, controllers applied 1 imported module unique within app. example, once using angular-ui bootstrap , on team added alertcontroller
. pretty confused when controller never hit, because angular-ui had defined controller.
so it's not question of visibility, of maintainability , naming.
everything defined on module public.
Comments
Post a Comment