【发布时间】:2017-02-23 03:55:10
【问题描述】:
我有一个组件,我想将它动态注入到我的 html 中。
我有一个这样的组件:
angular.module('test1', []);
angular.module('test1').component('test1', {
templateUrl: 'components/test1/test1.template.html',
controller: function test1Controller($scope) {
}
});
test1.template.html 文件如下所示:
<p>TEST 1</p>
在我的控制器上我有这个:
angular.module('myApp')
.controller('ctrlCtrl', function ($scope, $sce) {
$scope.tag = "<test1/>";
});
在我的index.html 上,我有这个:
<ng-bind-html ng-bind-html="tag"></ng-bind-html>
但标签不会显示。我尝试在ng-bind-html 字段上写字面意思"'<p>hi!</p>'",并写上“嗨!”出现在一个段落上,所以我认为这个错误不是因为错字。
我也尝试使用$sce.trustAsHtml,但也没有用:(
$scope.tag = $sce.trustAsHtml("<test1/>");
当我插入一个输入字段时,trustAsHtml 方法确实有效,但是当我尝试动态注入我的组件时,它就是不允许我,请帮助 D:
【问题讨论】:
-
ng-bind-html不编译指令。为什么要采取这种迂回的方式? -
真的吗?哎呀,太可怕了!...我有一个组件库,用户可以将这些组件拖到一个地方,以便创建一个包含所有这些组件的布局。基本上,我想动态创建组件来实现这一点:(
-
不难用
$compile自己编译 -
$compile 使用教程odetocode.com/blogs/scott/archive/2014/05/07/…
标签: angularjs components