[英]Using angular.element containing a directive appears to ignore the transcluded content
假设我有一个需要动态生成的指令:
var windowEl = angular.element("<window>Transcuded content....</window");
$compile(windowEl)(scope);
$element.append(windowEl);
发生的事是成功创建了myWindow指令并将其附加到dom,但是所包含的内容丢失了。 我确实有transclude: true
设置在指令对象中。 我在这里想念什么吗? 如果没有,关于解决方法的任何想法?
这是我的windowDirective.js
define(['app'], function (app) {
app.directive('window', function () {
return {
restrict: 'E',
transclude: true,
templateUrl: 'app/shared/windows/windowView.html',
link: function (scope, element, attrs) {
scope.closeWindow = function () {
$(".window").hide();
}
}
}
});
});
我检查了您的代码,并且似乎可以检查该示例,在这种情况下,我也是如此,唯一的不同是,如果您使用指令来动态注入window
应为:
angular.module('App').directive('myDirective', function () {
return {
restrict: 'E',
transclude: true,
templateUrl: 'my-directive.html',
controller: function ($scope,$compile,$element) {
var windowEl = angular.element("<window>Transcuded content....</window");
$compile(windowEl)($scope);
$element.append(windowEl);
}
}
});
但是,就像@ Andrew Eisenberg一样,我不建议您动态注入html,也许有更好的方法,但是我们需要更好地了解您的情况。 如果不起作用,请更新一个插头。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.