从变量动态导入组件(AngularJS) [英] Dynamically Import Component From Variable (AngularJS)

查看:58
本文介绍了从变量动态导入组件(AngularJS)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试基于变量加载组件,但出现未捕获的错误:模板解析错误".我将如何去做这样的事情?

I'm trying to load a component based off of a variable but I get a "Uncaught Error: Template parse errors". How would I go about doing something like this?

<app-{{ this.plugin.component }}></app-{{ this.plugin.component }}>

推荐答案

我认为不可能在模板上做到这一点,但是您当然可以在您的控制器/组件上做到这一点,尝试做类似的事情:

I don't think that is possible to do it on template, but you certainly can do it on your controller/component try to do something like:

var scope = $rootScope.$new();
var element = angular.element('<app' + variable + '></app' + variable + '>');
element = $compile(element)(scope);

要在模板中打印元素,您需要使用$sce:

To print the element in your template you'll need to use $sce:

如何您是否使用$ sce.trustAsHtml(string)在Angular 1.2+中复制ng-bind-html-unsafe

这篇关于从变量动态导入组件(AngularJS)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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