AngularJS: 删除优先级较低的指令?
AngularJS: Remove the directive of lower priority?
我正在编写一个可以通过编程方式删除 ng-disabled
的指令(例如:当当前用户没有编辑权限时,删除 ng-disabled
并直接添加静态 disabled
属性。 )
我看了official doc about $compile, it says I can specify priority
option to decide the compilation order. The source code of latest AngularJS 1.7.9显示ngDisabled
的优先级是100
,所以我这样写:
app.directive('removeNgDisabled', function ($compile) {
return {
priority: 101, // The priority of ngDisabled is 100
compile: function ($el, $attrs) {
const el = $el[0]
el.removeAttribute('ng-disabled') // ng-disabled should be removed before it is compiled... isn't it?!
}
}
})
<input ng-model="foo" ng-disabled="true" remove-ng-disabled=""/>
我不知道哪里错了,ng-disabled
确实从DOM中消失了,但是ng-disabled
仍然有效...
一旦 Angular 开始 $compile
该元素,就似乎无法删除已经存在于 HTML 元素上的指令。
The priority
is used to sort the directives before their compile
functions get called.
但这似乎并不意味着你可以在排序之前做任何事情。也就是说,当“higher-ordering”指令被编译时,lower-ordering指令已经被推入执行队列(我猜,我没有深入研究内部实现。)和已无法删除。
我没有深究AngularJS中directive机制的内部实现;但至少,在官方文档中,没有办法“取消”或“删除”现有指令。所以实现类似行为的唯一方法是自己实现一个自制的ng-disabled
。
我正在编写一个可以通过编程方式删除 ng-disabled
的指令(例如:当当前用户没有编辑权限时,删除 ng-disabled
并直接添加静态 disabled
属性。 )
我看了official doc about $compile, it says I can specify priority
option to decide the compilation order. The source code of latest AngularJS 1.7.9显示ngDisabled
的优先级是100
,所以我这样写:
app.directive('removeNgDisabled', function ($compile) {
return {
priority: 101, // The priority of ngDisabled is 100
compile: function ($el, $attrs) {
const el = $el[0]
el.removeAttribute('ng-disabled') // ng-disabled should be removed before it is compiled... isn't it?!
}
}
})
<input ng-model="foo" ng-disabled="true" remove-ng-disabled=""/>
我不知道哪里错了,ng-disabled
确实从DOM中消失了,但是ng-disabled
仍然有效...
一旦 Angular 开始 $compile
该元素,就似乎无法删除已经存在于 HTML 元素上的指令。
The
priority
is used to sort the directives before theircompile
functions get called.
但这似乎并不意味着你可以在排序之前做任何事情。也就是说,当“higher-ordering”指令被编译时,lower-ordering指令已经被推入执行队列(我猜,我没有深入研究内部实现。)和已无法删除。
我没有深究AngularJS中directive机制的内部实现;但至少,在官方文档中,没有办法“取消”或“删除”现有指令。所以实现类似行为的唯一方法是自己实现一个自制的ng-disabled
。