gpt4 book ai didi

javascript - Angular 指令链接函数永远不会运行

转载 作者:行者123 更新时间:2023-12-02 16:21:33 25 4
gpt4 key购买 nike

我遵循了ng-bbok的教程,在指令定义处插入了一个空的compile函数,然后插入了link函数。这样,link 函数中的代码就永远不会被执行。最后我发现这是因为空的 compile 函数,当我神奇地删除它时,link 被执行了。为什么会出现这样的情况呢?我使用的是 Angular 1.3

{
编译:函数(){},
链接:函数($范围,元素,属性){
var size = attribute.gravatarSize || 80;
var hash = md5.digest_s($scope.email.from[0]);
$scope.gravatarImage = url + hash + '?size=' + size;
}
}

最佳答案

您不能同时定义 compile 属性和 link。如果您想使用 compile 函数,您可以返回链接函数:

compile: function() {
return function($scope, element, attributes) {
var size = attributes.gravatarSize || 80;
var hash = md5.digest_s($scope.email.from[0]);
$scope.gravatarImage = url + hash + '?size=' + size;
}
}

或者定义 prepost (链接)函数:

compile: function compile(tElement, tAttrs, transclude) {
return {
pre: function preLink(scope, iElement, iAttrs, controller) { ... },
post: function postLink(scope, iElement, iAttrs, controller) { ... }
}
}

检查documentation

关于javascript - Angular 指令链接函数永远不会运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29061262/

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