angularjs指令动态html替换元素和ng if
我有自己的自定义指令,定义如下:angularjs指令动态html替换元素和ng if,angularjs,angularjs-directive,Angularjs,Angularjs Directive,我有自己的自定义指令,定义如下: .directive("myDirective", [ return { restrict: "E", replace: true, terminal: true, // otherwise we won't be able to compile a template controller: "MyCtrl", scope: { ... },
.directive("myDirective", [
return {
restrict: "E",
replace: true,
terminal: true, // otherwise we won't be able to compile a template
controller: "MyCtrl",
scope: {
...
},
compile: function (element, attrs) {
element.html("");
element.removeClass();
...
return {
post: function (scope, iElement, iAttrs, controller) {
loadTemplate(templUrl).then(function (templateElement) {
//iElement.replaceWith(templateElement);
iElement.append(templateElement);
var teLinkingFn = $compile(templateElement);
teLinkingFn(scope);
var modal = $("div.modal", templateElement);
if (modal.length > 0) {
scope._modal = modal;
templateElement.removeClass();
if (iAttrs.class) {
$("button", templateElement).eq(0).addClass(iAttrs.class);
}
}
});
...
现在,我将其与ng一起使用,如果:
<my-directive ... ng-if="some_criteria"/>
问题是当我替换了原始元素并且一些条件发生了变化时,我的自定义html仍然可见。但是如果我将附加到指令中,它就会工作(消失)。我不知道该怎么做。请提供帮助。当您使用模板文件中加载的任何内容替换原始标记
时,您将丢失原始标记中的ng if
指令。您可以在模板文件中添加ng if
,也可以不使用替换功能
您希望替换它有什么特别的原因吗?我以为所有属性(包括角度指令)都被复制了。我想我错了。。。正确的?