模块和AngularJS空间/名称冲突 [英] Modules and namespace / name collision in AngularJS

查看:205
本文介绍了模块和AngularJS空间/名称冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下jfiddle http://jsfiddle.net/bchapman26/9uUBU/29/

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

//angular.js example for factory vs service
var app = angular.module('myApp', ['module1', 'module2']);

var service1module = angular.module('module1', []);

service1module.factory('myService', function() {
    return {
        sayHello: function(text) {
            return "Service1 says \"Hello " + text + "\"";
        },
        sayGoodbye: function(text) {
            return "Service1 says \"Goodbye " + text + "\"";
        }
    };
});

var service2module = angular.module('module2', []);

service2module.factory('myService', function() {
    return {
        sayHello: function(text) {
            return "Service2 says \"Hello " + text + "\"";
        },
        sayGoodbye: function(text) {
            return "Service2 says \"Goodbye " + text + "\"";
        }
    };
});

function HelloCtrl($scope, myService) {
    $scope.fromService1 = myService.sayHello("World");
}

function GoodbyeCtrl($scope, myService) {
    $scope.fromService2 = myService.sayGoodbye("World");
}​

我有2个模块(模块和模块2)。这两个模块1和模块2定义一个名为为myService服务。这似乎在角上创建为myService名称冲突时,这两个模块导入到对myApp。看来AngularJs刚刚使用第二个服务定义,而不警告您可能的问题。

I have 2 modules (module1 and module2). Both module1 and module2 define a service called myService. This appears to create a name clash on myService within Angular when both modules are imported into myApp. It appears AngularJs just uses the second service definition without warning you of the possible issue.

非常大的项目(或一般只是重用模块)的名称将为冲突的风险,这可能是难以调试。

Very large projects (or just reusing modules in general) would have a risk of names clashing, which could be difficult to debug.

有没有办法来preFIX名称与模块名称,这样的名称冲突不会发生?

Is there a way to prefix names with the module name so that name clashes don't happen?

推荐答案

截至今日,AngularJS模块不提供任何形式的命名空间,将美元的不同模块的物体夹住的p $ pvent碰撞。原因是,一个AngularJS应用程序有一个适用于所有对象的名称不尊重模块名称单个喷射。

As of today, AngularJS modules do not provide any sort of namespacing that would prevent collisions between objects in different modules. The reason is that an AngularJS app has a single injector that holds names for all objects without respect to module names.

借助 AngularJS开发指南说:

要管理的依赖创造的责任,每个角
  应用程序有一个注射器。注射器是一种服务定位器是
  负责依赖的建设和查找。

To manage the responsibility of dependency creation, each Angular application has an injector. The injector is a service locator that is responsible for construction and lookup of dependencies.

正如你所提到的,注射模块到主/应用模块时,可能会导致讨厌的错误。当碰撞发生他们是沉默的,获奖者由哪个是最后一个模块注入的决定。

As you've mentioned, nasty bugs can result when injecting modules into your main/app module. When collisions happen they are silent and the winner is determined by whichever was the last module injected.

因此​​,没有,有没有一个内置的避免这些冲突的方法。也许这将在未来出现。对于大型的应用程序在哪里这个问题变得更加容易,你是正确的,命名约定是你的最佳工具。考虑属于一个模块或功能区的对象是否会使用短preFIX。

So no, there's not a built in way of avoiding these collisions. Maybe this will happen in the future. For large apps where this problem becomes more likely, you're right that naming conventions are your best tool. Consider whether objects belonging to a module or function area might use a short prefix.

这篇关于模块和AngularJS空间/名称冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆