AngularJS:无论ng-if条件如何,加载指令模板
发布时间:2020-12-17 17:45:13 所属栏目:安全 来源:网络整理
导读:我正在使用Directives将我的代码分成模板,我希望这些模板基于if条件加载.现在,当我查看网络流量时,Angular会将所有模板发送到客户端,无论是否满足ng-if条件. div class="container" ul template1 ng-if="taskCategory == 'condition1'"/template1 template2
我正在使用Directives将我的代码分成模板,我希望这些模板基于if条件加载.现在,当我查看网络流量时,Angular会将所有模板发送到客户端,无论是否满足ng-if条件.
<div class="container"> <ul> <template1 ng-if="taskCategory == 'condition1'"></template1> <template2 ng-if="taskCategory == 'condition2'"></template2> <template3 ng-if="taskCategory == 'condition3'"></template3> </ul> </div> 以下是我的一个指令模板的示例: /* Directive Template */ .directive('template1',[function() { return { restrict: 'E',templateUrl: 'view/templates/template1.tpl.html' }; }]) 这是预期的行为吗?它按预期可视化工作.但是我的印象是ng-if数据会根据条件延迟加载.或者我误解了如何使用ng-if? 解决方法
这绝对是预期的行为. ng-if将根据条件有条件地从DOM中删除元素.该元素在被删除之前仍然被渲染(否则元素的克隆被添加回DOM).
该行为在AngularJS: ngif文档的第一段中说明. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |