小编Ume*_*a D的帖子

Angular 5 将动态 html 文件添加到 DIV

我对 Angular 很陌生,我试图将 html 文件作为我的字符串插入并插入到 DIV 元素中

我有我的search.component.html电话

<div #ID></div>
Run Code Online (Sandbox Code Playgroud)

组件.ts

import { Component} from '@angular/core';

@Component({
  selector: 'app-search',
  templateUrl: './search.component.html',
  styleUrls: ['./search.component.scss']
})
export class SearchComponent {
  constructor() {}

  let ServerResponseHtml = '<div><input type="text"/><input type="text"/><span class="btn btn-outline-primary btn-sm" (click)="open(content)">View Document</span></div>';

  document.getElementById("DIV").innerHTML = ServerResponseHtml;
}
Run Code Online (Sandbox Code Playgroud)

我从服务器获取响应作为完整的 html 标记,我只需要附加到我的 DOM 中并显示内容,标记也可以具有内联样式。

我试过了,<div [innerHTML]="ServerResponseHtml"></div> and <div innerHTML="{{ServerResponseHtml}}"></div>但这不是显示为 html,而是显示为文本。

html angular

4
推荐指数
1
解决办法
9728
查看次数

标签 统计

angular ×1

html ×1