角度2-是否可以在不删除HTML内容的情况下将应用程序组件绑定到现有DOM中? [英] Angular 2 - It is possible to bind the app component in existing DOM without erasing HTML content?

查看:46
本文介绍了角度2-是否可以在不删除HTML内容的情况下将应用程序组件绑定到现有DOM中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在预先执行的HTML文档中引导我的主要角度组件. 这是我现有的DOM:

I try to bootstrap my main angular component in a pre-executed HTML Document. This is my existing DOM :

<html>
<head></head
<body>

<!-- this is my template generated with Symfony -->
<div ...>
 <ul>
   <li><angularComponent></angularComponent></li>
   <li><angularComponent></angularComponent></li>
 </ul>
</div>

</body>
</html>

我想在主体或主div上绑定我的主要应用程序组件(或指令),而不用替换我预先生成的内容(用symfony).

I want to bind my Main App Component (or Directive) on body, or on the main div, without replacing my pre generated content (with symfony).

有可能吗?还有其他解决方案吗?

It is possible ? Have you other solution ?

谢谢

推荐答案

好,所以我还没有找到这个问题的好答案,这是我做的(拙劣的)方法:

Ok, so i haven't found any good answers to this question, here's the (hacky) way I did it:

document.getExistingContentByTag = function(tagName){
    var target = document.getElementsByTagName(tagName)[0];

    if(!target || !target.tagName) return '';

    return target.innerHTML;
}

并渲染模板:

@Component({
selector: 'my-app',
template: document.getExistingContentByTag('my-app')
})

因此它提取my-app标签的内容,发挥其魔力并将其写回.

So it pulls the content of the my-app tag, does its magic and writes it back.

这篇关于角度2-是否可以在不删除HTML内容的情况下将应用程序组件绑定到现有DOM中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆