gpt4 book ai didi

javascript - AngularJS : How can I compile a directive as another directive?

转载 作者:行者123 更新时间:2023-11-28 01:14:56 27 4
gpt4 key购买 nike

我有可以编译的 Angular 指令

  1. <greeting /><print-greeting />

  2. <print-greeting />Hello World!

我怎样才能把greeting在我的 HTML 中标记并将其编译为 print-greeting然后最后显示Hello World! ?目前它在变成 print-greeting 后停止。 .

这是我的代码:Plunker .

<小时/>

从上述骗子复制的指令:

greeting指令

// Transforms <greeting /> into <print-greeting />
app.directive("greeting", function ($compile) {
return {
restrict: 'E',
priority: 2,
compile: function ($templateElement, $templateAttributes) {

var template = '<print-greeting />';
$templateElement.replaceWith(template);

return function LinkingFunction($scope, $element, $attrs) { };
}
};
});

print-greeting指令

// Transforms <print-greeting /> into "Hello World!"
app.directive("printGreeting", function ($compile) {
return {
restrict: 'E',
priority: 1,
compile: function ($templateElement, $templateAttributes) {
var template = 'Hello World!';
$templateElement.replaceWith(template);

return function LinkingFunction($scope, $element, $attrs) { };
}
};
});

最佳答案

您正在对已编译的模板进行更改。为了使辅助指令被编译,您需要重新编译:

return function LinkingFunction($scope, $element, $attrs) {
$compile($element);
};

Fork of your Plunker

关于javascript - AngularJS : How can I compile a directive as another directive?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23959130/

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