且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

指令链接功能无权访问整个模板DOM

更新时间:2023-12-05 15:22:58

通过尝试在指令的link()方法中进行DOM操作,你可以'重新尝试查询/修改尚未呈现的DOM的一部分。

By attempting to do DOM manipulation in a directive's link() method like that, you're trying to query/modify a part of the DOM that hasn't been rendered yet.

您需要将这些jquery调用推迟到稍后。你可以这样做:

You need to defer those jquery calls until later. You can do this using:

$scope.$evalAsync(function() {
  // DOM code
});

$timeout(function() {
 // DOM code
}, 0);

使用 $ evalAsync 将在运行期间运行表达式下一个 $ digest 循环,允许您在浏览器中呈现之前修改HTML。使用 $ timeout 将等到所有 $ digest 周期完成。

Using $evalAsync will run the expression during the next $digest cycle, will allow you to modify HTML before it's rendered in the browser. Using $timeout will wait until all $digest cycles are complete.