Angular 6:如何将html文件包含到组件中?

问题描述 投票:1回答:1

在servlet环境中,我们可以包含这样的任何jsp:

<%@include file=" 'path-to-jsp' " %>

在我的角度项目中,我有一个包含许多组件的html表单。我想将此表单包含在2个组件中(创建和更新)。

user-form.html:

<input type=".."...>
<input type=".."...>
<input type=".."...>
<select>...

创建-user.component.html:

<form>
   // insert content of 'user-form.html' at compile time
   <button>Create User</button>
</form>

更新-user.component.html:

<form>
   // insert content of 'user-form.html' at compile time
   <button>Update User</button>
</form>

有没有办法在Angular 2+中完成这个?

注意:我使用反应形式

angular
1个回答
1
投票

你可以在create-user.component中使用[innerHtml]

@Component({
    selector: 'my-template',
    template: `
<div [innerHtml]="myTemplate">
</div>
`})
export public class MyTemplate {
    private myTemplate: any = "";
    @Input() url: string;
    constructor(http: Http) {
        this.myTemplate = yourHTMl); //Load it using http
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.