Angular 指令 DOM 操作依赖于作用域变量
Angular directive DOM manipulation dependent on scope variables
我读到 angular 中的 DOM 操作应该在指令的编译函数中完成,而不是在 pre-link/post-link/controller 中完成。编译函数无权访问范围。
我的问题是我想进行 DOM 依赖于范围变量的操作。例如,我有一个要传递给指令的列表。在指令中,我正在创建一个自定义的 select,其中包含列表项。在这种情况下,操纵 DOM 的正确位置在哪里?
请注意,我没有使用 ng-repeat - 我发现当列表变大时它非常慢。
不知道你在哪看过"DOM manipulation in angular should be done in the compile function of a directive"。这与 AngularJS 团队的建议相矛盾。
Creating a Directive that Manipulates the DOM
Directives that want to modify the DOM typically use the link
option to register DOM listeners as well as update the DOM.
-- AngularJS Developer Guide - Directives - DOM Manipulation
内置指令 ng-repeat
、ng-if
、ng-when
等都在 link
函数中进行 DOM 操作。
compile
The compile function deals with transforming the template DOM. Since most directives do not do template transformation, it is not used often.
我读到 angular 中的 DOM 操作应该在指令的编译函数中完成,而不是在 pre-link/post-link/controller 中完成。编译函数无权访问范围。
我的问题是我想进行 DOM 依赖于范围变量的操作。例如,我有一个要传递给指令的列表。在指令中,我正在创建一个自定义的 select,其中包含列表项。在这种情况下,操纵 DOM 的正确位置在哪里?
请注意,我没有使用 ng-repeat - 我发现当列表变大时它非常慢。
不知道你在哪看过"DOM manipulation in angular should be done in the compile function of a directive"。这与 AngularJS 团队的建议相矛盾。
Creating a Directive that Manipulates the DOM
Directives that want to modify the DOM typically use the
link
option to register DOM listeners as well as update the DOM.-- AngularJS Developer Guide - Directives - DOM Manipulation
内置指令 ng-repeat
、ng-if
、ng-when
等都在 link
函数中进行 DOM 操作。
compile
The compile function deals with transforming the template DOM. Since most directives do not do template transformation, it is not used often.