在 AngularJS 中,如果我在两个模块中注册两个同名的不同指令,最后注册的会赢吗? IIUC Angular 模块不提供命名空间。
此外,Angular 中的模块是否提供了除了方便的注入(inject)器配置分组以外的任何东西?
换句话说 - 你能在运行时检测到某些东西(比如指令)来自哪个模块吗?
编辑:关于这个问题的第一部分:我认为会。 https://docs.angularjs.org/guide/module
最佳答案
AngularJS, if I register two different directives with the same name in two modules, will the last registration win?
这将是一个“领带”。它们都被注册并且它们都被执行。具有相同priority
的指令的执行顺序与其定义的顺序相同,而link
函数将以相反的顺序执行。
如果他们不满足某些条件,即
- Multiple directives requesting isolated scope.
- Multiple directives publishing a controller under the same name.
- Multiple directives declared with the transclusion option.
- Multiple directives attempting to define a template or templateURL.
...然后是 respective error被抛出。请注意,文档在“隔离范围”上不准确,实际上是“新范围”,禁止同时使用 scope: true
和 scope: { ... }
.每个元素最多一个范围。
Put another way - can you detect at runtime which module something (say a directive) came from?
不,不是没有破解 Angular。但是如果存在指令冲突,multidir
错误会告诉您模块名称。
关于javascript - 如果我在两个模块中注册了两个同名的不同指令,最后注册的会赢吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31702149/