不,没有什么好的声明方式可以告诉Angular在将
x属性移植到模板时应如何合并或操纵属性。
Angular实际上将属性从源直接复制到目标元素(有一些例外),并合并属性值。您可以
mergeTemplateAttributes在Angular编译器的函数中看到此行为。
由于您无法更改该行为,因此可以使用指令定义的
compile或
link属性来控制属性及其值。在编译阶段而不是在链接阶段进行属性操作对您来说更有意义,因为您希望这些属性在任何链接函数运行时都“准备就绪”。
您可以执行以下操作:
.directive('foo', function() { return { // .. compile: compile // .. }; function compile(tElement, tAttrs) { // destination element you want to manipulate attrs on var destEl = tElement.find(...); angular.forEach(tAttrs, function (value, key) { manipulateAttr(tElement, destEl, key); }) var postlinkFn = function(scope, element, attrs) { // your link function // ... } return postlinkFn; } function manipulateAttr(src, dest, attrName) { // do your manipulation // ... }})


