简体   繁体   English

Angular 7:动态生成模板

[英]Angular 7: Dynamically generate template

I'm rewriting my website https://ixtutor.com/ completely in angular.我正在完全用 angular 重写我的网站https://ixtutor.com/ I've created few components that render as widgets on a blog page.我创建了几个在博客页面上呈现为小部件的组件。

For example, on a blog post https://ixtutor.com/tensorflow-basics-with-housing-prices-prediction-example-chapter-i/ , there are widgets like, (i) Code runner widget例如,在博客文章https://ixtutor.com/tensorflow-basics-with-housing-prices-prediction-example-chapter-i/ 上,有一些小部件,例如,(i)代码运行小部件 (ii) Exercise widget (ii) 练习小工具在此处输入图片说明

I'm modeling these widgets as components in my angular application.我正在将这些小部件建模为我的 angular 应用程序中的组件。

Problem : One of the page in my application is a live html editor, which allows an author to write the (angular) html templates in a textarea T1 and see the (rendered) view side-by-side.问题:我的应用程序中的一个页面是一个实时 html 编辑器,它允许作者在 textarea T1 中编写(有角度的)html 模板并并排查看(渲染的)视图。 I also want the author to be able to use the widget components in the html that they create.我还希望作者能够在他们创建的 html 中使用小部件组件。

This works great with regular html tags, where I can use the innerHTML property of a div to directly bind the ngModel of the textarea T1, however, it doesn't work for the angular components.这适用于常规 html 标签,我可以使用 div 的 innerHTML 属性直接绑定 textarea T1 的 ngModel,但是,它不适用于角度组件。

I've looked at dynamic angular components but I cannot figure out how to use that in a full-fledged html template.我看过动态角度组件,但我无法弄清楚如何在成熟的 html 模板中使用它。 Any recommendations or suggestions will be really appreciated.任何建议或建议将不胜感激。

Thanks.谢谢。

Angular does not support inserting dynamic components in the template at runtime. Angular 不支持在运行时在模板中插入动态组件。 It has to know at compile time which components will be inserted.它必须在编译时知道将插入哪些组件。

The only way is to compile your Angular components to web components using Angular elements , that way, you will be able to insert them as custom HTML elements in your application.唯一的方法是使用Angular 元素将 Angular 组件编译为 Web 组件,这样,您就可以将它们作为自定义 HTML 元素插入到应用程序中。

You will have to develop them in a separate library for this.为此,您必须在单独的库中开发它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Angular 11 中动态生成/生成现有组件 - spawn/generate existing components dynamically in Angular 11 如何在Angular模板中动态更改属性名称? - How to dynamically change attribute name in Angular template? 在Angular指令中动态更新模板的HTML - Dynamically updating the template's Html in an Angular directive 在组件中动态加载角度模板字符串 - Dynamically load angular template string in component 在 Component 装饰器中动态加载 Angular 模板 - Dynamically load Angular template in `Component` decorator 如何使用 Angular 中的模板参考动态渲染 ng-template - How to render ng-template dynamically with Template Reference in Angular 使用django类基本视图动态生成类别列表到基本模板中 - dynamically generate category list into base template with django class base views 使用HTML元素作为“模板”还是在javascript中动态生成HTML更好? - Is it better to use an HTML element as a “template” or to generate the HTML dynamically in the javascript? 如何使用 ngFor 动态生成 HTML 表。 在 Angular - How To dynamically generate an HTML Table using ngFor. in Angular Angular 如何在没有选择器标签的情况下动态生成组件 - Angular how to generate dynamically component without the selector tag
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM