【问题标题】:Ionic v2 bind html离子 v2 绑定 html
【发布时间】:2017-09-17 19:11:43
【问题描述】:

我正在尝试在视图文件中绑定一些文本和 html 数据,但该页面在某些设备中无法打开。 我试图用<div> 标签包围html,但仍然无法正常工作。 我想知道在 angular2 或 ionic 2 中是否有 $sce.trustashtml 的替代品。

我在下面添加了示例代码

<div [innerHTML]="html-variable-from-controller"></div>

我试过[inner-html] 还是不行

任何帮助将不胜感激

【问题讨论】:

标签: angular ionic2


【解决方案1】:

我的应用也发生了这种情况。在我的例子中,innerHTML 属性正在修剪音频和 iframe 元素。

我首先为该 HTML 元素创建了一个局部变量,如下所示:

<p padding #post_content></p>

这里#post_content 创建了一个post_content 局部变量。

然后在 TypeScript 中使用 @ViewChild Decorator 引用特定元素,如下所示:

@ViewChild('post_content') post_content;

并在 TypeScript 中分配 HTML 如下:

this.post_content.nativeElement.innerHTML = this.htmlContent;

【讨论】:

  • 感谢您的帮助,但在我的情况下,#post_content 变量将位于幻灯片框内,并且可以有 n 个具有不同值的幻灯片。
  • @user3660436 你可以为你的代码创建一个codepen/plunkr吗?
【解决方案2】:

使用离子标签代替 div,实例:

<ion-content [innerHTML]="html-variable-from-controller"></ion-content>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-26
    • 1970-01-01
    • 2015-12-14
    • 2019-02-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多