如何使用从后端检索的字符串作为Angular2中的模板? [英] How to use string retrieved from back-end as template in Angular2?

查看:79
本文介绍了如何使用从后端检索的字符串作为Angular2中的模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Angular2应用中,我想使用ajax从后端检索html标签字符串,并将其用作Angular2中的模板.

In an Angular2 app, I'd like to use ajax to retrieve a html tag string from back-end and use it as template in Angular2.

// The str I get from server by ajax is:
var str = "<my-hero [hero]=\"hero\"></my-hero>"

该应用程序已在浏览器中运行时.如果我$('myHeroParent').append(str).此代码无效.

When the app is already running in the browser. If I $('myHeroParent').append(str). This code won't work.

您如何进行这项工作?它称为动态加载组件吗?

How can you make this work? Is it called dynamic loading component?

推荐答案

将字符串传递给$compile而不是模板,以创建DOM元素:

Instead of a template, pass the string to $compile to create a DOM element: https://docs.angularjs.org/api/ng/service/$compile

这篇关于如何使用从后端检索的字符串作为Angular2中的模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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