简体   繁体   English

AngularJS-与标准html元素名称相同的自定义指令

[英]Angularjs - custom directive with the same name of standard html elements

What happens if I create a directive that has the same name of standard html attributes or elements? 如果创建的指令与标准html属性或元素名称相同,会发生什么情况? For example if I define: 例如,如果我定义:

.directive('link', function(){
  return {
    restrict: 'A',
    link: function(scope, elements, attributes){
      // do something
   }
})

used like this 这样使用

<a link="http://www.foo.com" ng-href="www.google.com">Conditional Link </a>

would it conflict with the standard <link rel="stylesheet" type="text/css" href="theme.css"> element? 是否会与标准<link rel="stylesheet" type="text/css" href="theme.css">元素<link rel="stylesheet" type="text/css" href="theme.css">

Maybe in this case it would not because I've restricted the directive to attributes only, but my question is more general. 也许在这种情况下,这不是因为我仅将指令限制为属性,而是我的问题更笼统。

For example what if I named my directive href ? 例如,如果我将指令命名为href怎么办?
Will the directives always have the precedence over the standard html element or attributes? 指令是否始终优先于标准html元素或属性?

Directives can attach behavior to existing html element. 指令可以将行为附加到现有的html元素。 If you create directive href, i believe there should be no issue, unless your directive removes itself (or href ). 如果您创建指令href,我相信应该没有问题,除非您的指令将自身删除(或href )。 There is no concept of precedence unless multiple directives are defined or applied on same element. 没有优先级的概念,除非在同一元素上定义或应用了多个指令。

Remember you cannot change the behavior of browser, if browser sees a href with a remote url it would make a request irrespective. 请记住,您无法更改浏览器的行为,如果浏览器看到带有远程URL的href,则无论如何都会发出请求。

AngularJS itself uses <input /> as a directive and it works fine with the standard input behavior intact. AngularJS本身使用<input />作为指令,并且可以在不影响标准输入行为的情况下正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM