gpt4 book ai didi

javascript - 访问 Angular Directive(指令)中的嵌套节点

转载 作者:行者123 更新时间:2023-12-03 10:51:36 25 4
gpt4 key购买 nike

我有以下 sudo html

<foo>
<span>Bar</span>
</foo>

和指令:

myapp.directive('foo', function () {
return {
restrict: 'E',
replace: true,
transclude: true,
template: '<div><p>{{usedToBeInSpan}}</p></div>'
}
});

如何提取嵌套 span 节点的内容并将其在指令范围内的内容设置为 usedToBeInSpan

请注意,我实际上并没有使用它来替换嵌套标签,它只是一个简化的示例

最佳答案

您可以在指令中添加链接函数,如下所示:

myapp.directive('foo', function () {
return {
restrict: 'E',
replace: true,
transclude: true,
template: '<div><p>{{usedToBeInSpan}}</p></div>',
link: function (scope, element, attrs) {
// the element argument contains the html content inside the directive tags <foo></foo>
scope.usedToBeInSpan = "";//populate from "element"
}
}
});

关于javascript - 访问 Angular Directive(指令)中的嵌套节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28406718/

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