ng-docs.github.io icon indicating copy to clipboard operation
ng-docs.github.io copied to clipboard

angular1x 有 $compile angular6 有什么方法可以编译带指令的字符串?

Open YeahPotato opened this issue 5 years ago • 6 comments

var e = angular.element('<div directive></div>');
element.append(e);
$compile(e)($scope);

angular6 中有没有类似的方法;

<div [innerHTML]="htmlString"></div>
var htmlString = '<span ngFor="let i of [1,2,3]"></span>'

有什么方法可以解析ng 指令?

YeahPotato avatar Mar 25 '19 09:03 YeahPotato

在 AoT 模式下,Angular 应用是不附带编译器模块的,因此这个是做不到的。如果用 JIT 模式,带来的坏处可能会大于好处,也没必要。 你可以说说你打算解决什么问题,有很多更好地方案可以做到。

asnowwolf avatar May 05 '19 04:05 asnowwolf

在 AoT 模式下,Angular 应用是不附带编译器模块的,因此这个是做不到的。如果用 JIT 模式,带来的坏处可能会大于好处,也没必要。 你可以说说你打算解决什么问题,有很多更好地方案可以做到。

就是想输出html但是html里包含指令,想什么方法可以解析ng指令;

YeahPotato avatar May 07 '19 10:05 YeahPotato

@YeahPotato https://stackblitz.com/edit/angular-moqyep?file=src%2Fapp%2Fapp.component.ts

vthinkxie avatar Jul 25 '19 06:07 vthinkxie

@YeahPotato 可以使用 Angular Elements,也就是 Web Components 的 Angular 实现。它可以解析模板中的组件。Angular 官方文档就是用它来支持文档中内嵌的组件的。

asnowwolf avatar Jul 26 '19 00:07 asnowwolf

@YeahPotato https://stackblitz.com/edit/angular-moqyep?file=src%2Fapp%2Fapp.component.ts

感谢。

YeahPotato avatar Jul 30 '19 06:07 YeahPotato

@YeahPotato 可以使用 Angular Elements,也就是 Web Components 的 Angular 实现。它可以解析模板中的组件。Angular 官方文档就是用它来支持文档中内嵌的组件的。

好的,谢谢。问题已解决了。

YeahPotato avatar Jul 30 '19 06:07 YeahPotato