【问题标题】:Function not calling added with document.getElementById('someId').appendChild()未使用 document.getElementById('someId').appendChild() 添加的函数
【发布时间】:2018-12-14 21:07:13
【问题描述】:

我从 api 获取 HTML 代码的一小部分,想要渲染到 DOM 中,如下所示...

// getting template from backend.
let template1 = "<div class='well'><formio src='example/text.json'></formio>"; //appearing into dom, but not loading form.

// second way
let template2 = "<formio [src]='getUrl()'></formio></div>";
// calling getUrl from component.ts file.

// attaching template into DOM using appendChild() method..
const fragment = document.createRange().createContextualFragment(template);
document.getElementById('landingTemplate').appendChild(fragment);

getUrl() {
   return 'example/text.json';
}
// getUrl() not calling from HTML.
&lt;div id="landingTemplate"&gt;&lt;/div&gt;

所以无法渲染formio。任何人有想法请帮助我。

【问题讨论】:

  • 没有真正的理由使用document.createRange().createContextualFragment(template);,只需按照下面提到的LGSon。
  • 嗯,发布了一个答案,但我说得对吗,它确实添加了标记,但您的 formio 组件不起作用?
  • 如果是这样,我添加了一些可能会有所帮助的链接,其中一个甚至可能是重复的......
  • document.createRange().createContextualFragment() 和 innerHtml 都给出了相同的结果。但是问题是formio没有通过这种方式渲染,而formio直接放入html时工作正常。

标签: javascript html angular


【解决方案1】:

如果字符串中有 HTML 标记,请使用 innerHTML

document.getElementById('landingTemplate').innerHTML = template;

更新

如果它确实添加了标记但您的 formio 组件不起作用? ...那么这里有几个链接可能是您正在寻找的:

【讨论】:

  • 但是,template 是在反引号 (es6) 中构造的。如果您使用 .innerHTML,这会起作用吗?
  • @DavidR 是的,它会的。反引号用于模板文字,尽管它仍然是一个字符串
  • 感谢@LGSon 的澄清!
猜你喜欢
  • 2011-03-22
  • 1970-01-01
  • 1970-01-01
  • 2015-11-03
  • 2023-04-08
  • 1970-01-01
  • 2017-10-20
  • 2019-08-07
  • 1970-01-01
相关资源
最近更新 更多