AngularJS:无论ng-if条件如何,加载指令模板

Nei*_*eil 5 angularjs

我正在使用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>
Run Code Online (Sandbox Code Playgroud)

以下是我的一个指令模板的示例:

/* Directive Template */
.directive('template1', [function() {
    return {
        restrict: 'E',
        templateUrl: 'view/templates/template1.tpl.html'
    };
}])
Run Code Online (Sandbox Code Playgroud)

这是预期的行为吗?它按预期可视化工作.但我的印象是ng-if数据会根据条件延迟加载.或者我误解了如何使用ng-if

Jus*_*ner 6

这绝对是预期的行为.ng-if将根据条件有条件地从DOM中删除元素.该元素在被删除之前仍然被渲染(否则元素的克隆被添加回DOM).

该行为在AngularJS:ngif文档的第一段中说明.