angular - 将 Angular 组件动态转换为 HTML

标签 angular angular-template

我希望在元素内单击按钮时呈现模板,我已将该模板创建为单独的组件(自定义组件),因为它可以多次使用。因此,当我单击希望将此自定义组件编译为 HTML 字符串返回时,因为我需要使用该字符串处理某些内容并发送 html 字符串。有 Angular 吗?

示例代码(只是为了解释我的情况)

import { templatecomponent } ./template.component

...

onButtonClick(): any
{
  return compile(templatecomponent) // return the html from the template component
}

最佳答案

我认为你不能像上面那样使用它!您有用于组件和条件检查的选择器。您可以使用条件检查来检查按钮的单击情况,如果为 true,您的组件将被调用

Custom.component.ts 

    @Component({
       selector: "custom-app",
       template: `I am from custom component`,
       style: ""
    })

Calling.component.ts

     @Component({
           selector: "calling-app",
           template: `<button (click)="customFn()">Show custom</button>
                       <custom-app *ngIf="isCustom"></custom-app>
                      `,
           style: ""
     })

    export class CallingComponent{
            isCustom: false
            constructor(){}
            customFn(){
                this.isCustom =  !this.isCustom
            }
     }

不要忘记配置模块

关于angular - 将 Angular 组件动态转换为 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57309531/

相关文章:

css - Ionic 2 动画(使用 Angular2)=> 来自 variable.scss 的颜色, `Failed to execute ' animate' on 'Element' : Partial keyframes are not supported

angular - 身份验证 - VS 2017 15.3; ASP.Net Core 2.0; Angular

javascript - AngularJs 指令 - &lt;script&gt; inside template

angular - 在多个位置输出/渲染模板,与 ngIf 条件分开

Angular MSAL AD 401 在 Angular 应用程序中未经授权,但在 Postman 中 200 正常 - ASP.NET Core Web API

angular - 如何在angular 2中将按钮添加到列到ng2-table

Angular 5 - 动态地为特定元素添加样式

angular - 即使没有做任何更改,Angular 也会无限重新渲染是否正常?

angular5 - Angular 模板呈现非编码 URL

angular - 错误 TS2339 : Property 'scan' does not exist on type 'Observable<Message[]>'