gpt4 book ai didi

angularjs - 如何嵌入到属性中?

转载 作者:行者123 更新时间:2023-12-02 21:42:45 24 4
gpt4 key购买 nike

是否可以以某种方式使用 ngTransinclude 作为属性值,而不是替换内部 HTML 内容?例如这个简单的指令

var testapp = angular.module('testapp', [])

testapp.directive('tag', function() {
return {
template: '<h1><a href="{{transcludeHere}}" ng-transclude></a></h1>',
restrict: 'E',
transclude: true
}
});

并将其用作

<tag>foo</tag>

我希望它翻译成

<h1><a href="foo">foo</a></h1>

有什么方法可以做到这一点,或者我是否必须使用属性而不是嵌入?

这是 fiddle举个例子

最佳答案

类似这样的事情:

var testapp = angular.module('testapp', [])

testapp.directive('tag', function() {
return {
restrict: 'E',
template: '<h1><a href="{{transcluded_content}}">{{transcluded_content}}</a></h1>',
replace: true,
transclude: true,
compile: function compile(tElement, tAttrs, transclude) {
return {
pre: function(scope) {
transclude(scope, function(clone) {
scope.transcluded_content = clone[0].textContent;
});
}
}
}
}
});​

fiddle .

关于angularjs - 如何嵌入到属性中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11703086/

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