javascript - 如果我在两个模块中注册了两个同名的不同指令,最后注册的会赢吗?

标签 javascript angularjs

在 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: truescope: { ... } .每个元素最多一个范围。

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/

相关文章:

javascript - 提交后如何保留下拉列表的值

javascript - 如何在 javascript 中更改 HTML 对象元素数据属性值

javascript - 使用 javascript 进行 Bootstrap 模式调用,最佳实践

javascript - 即兴 - 简单地将变量传递给 URL

javascript - 使用 Angular js 嵌入 Html 页面

javascript - Angular UI 网格 : Hover on expandable row

angularjs - 托管在 Pivotal Web 服务中的 Spring RESTful 没有 'Access-Control-Allow-Origin' 错误

javascript - 移相器、圆形按钮

javascript - Selenium WebDriver - 等待 Angular 请求(Java)

angularjs - angularjs elasticsearch typeahead匹配未定义