如果我在两个模块中注册了两个具有相同名称的不同指令,那么最后一次注册会成功吗? [英] If I register two different directives with the same name in two modules, will the last registration win?

查看:76
本文介绍了如果我在两个模块中注册了两个具有相同名称的不同指令,那么最后一次注册会成功吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在AngularJS中,如果我在两个模块中注册了两个具有相同名称的不同指令,那么最后一次注册会成功吗? IIUC Angular模块不提供命名空间.

In AngularJS, if I register two different directives with the same name in two modules, will the last registration win? IIUC Angular modules provide no namespacing.

此外,Angular中的模块是否提供除喷射器配置的方便分组以外的其他功能?

Also, do modules in Angular provide anything other than a convenient grouping of configuration of the injector?

采用另一种方式-您可以在运行时检测某个模块(例如一条指令)来自哪个模块吗?

Put another way - can you detect at runtime which module something (say a directive) came from?

关于这个问题的第一部分:我认为会的. https://docs.angularjs.org/guide/module

re the first part of this question: I think it will. https://docs.angularjs.org/guide/module

推荐答案

AngularJS,如果我注册两个具有相同名称的不同指令 在两个模块中,最后一次注册会赢吗?

AngularJS, if I register two different directives with the same name in two modules, will the last registration win?

这将是一个领带".它们都被注册并且都被执行.具有相同priority的指令的执行顺序与定义它们的顺序相同,并且link函数将以相反的顺序执行.

It will be a 'tie'. Both of them are registered and both of them are executed. The execution order for directives with the same priority is the same as the order in which they were defined, and link functions will be executed in reverse order.

如果他们不满足某些条件,即

If they don't meet certain conditions, i.e.

  • 多个指令要求隔离范围.
  • 多个指令以相同的名称发布控制器.
  • 使用包含选项声明的多个指令.
  • 尝试定义模板或templateURL的多个指令.
  • 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.

...然后抛出一个相应的错误.请注意,文档在隔离范围"上是不准确的,实际上是新范围",禁止同时使用scope: truescope: { ... }.每个元素最多一个作用域.

...then a respective error is thrown. Note that the documentation is inaccurate on 'isolated scope', it is 'new scope' in fact, both simultaneous scope: true and scope: { ... } are prohibited. One scope max per element.

采用另一种方式-您是否可以在运行时检测哪个模块 (例如一条指令)来自哪里?

Put another way - can you detect at runtime which module something (say a directive) came from?

不,不是没有Angular的黑客.但是multidir错误会告诉您模块名称是否存在指令冲突.

No, not without hacking Angular. But multidir error will tell you module names if there are directive collisions.

这篇关于如果我在两个模块中注册了两个具有相同名称的不同指令,那么最后一次注册会成功吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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