gpt4 book ai didi

javascript - AngularJS 指令 DOM 操作不执行 link()

转载 作者:行者123 更新时间:2023-12-02 16:48:55 24 4
gpt4 key购买 nike

尝试进行一些 DOM 操作,但 link() 从未被调用,因此什么也没有发生:

app.js

var app = angular.module('app', ['directives', ...]);
var directives = angular.module('directives', []);
...

directives.js

directives.directive('doIt', ['$window', function($window) {
return {
restrict: 'A',
link: function(scope, element, attrs) {
console.log('Inside link()');
// Do stuff with $window
}
};
]});

HTML

<html ng-app="app">
<body>
<div ng-view>
<div do-it>
// ....
</div>
</div>
</body>
</html>

我错过了什么?

最佳答案

你的方括号不正确

directives.directive('doIt', ['$window', function($window) {
return {
restrict: 'A',
link: function(scope, element, attrs) {
console.log('Inside link()');
// Do stuff with $window
}
};

}]);//Check this part. The square bracket was on the wrong side of }

像上面那样移动它

您的模块减速中还有一些额外的周期

var app = angular.module('app', ['directives']);//删除多余的....

关于javascript - AngularJS 指令 DOM 操作不执行 link(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26858485/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com