AngularJS:指令模板无论装载NG-如果条件 [英] AngularJS: Directive templates loading regardless of ng-if condition

查看:80
本文介绍了AngularJS:指令模板无论装载NG-如果条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的指令到我的code分离成模板,我想这些模板基于负载上的,如果条件。现在,当我看着网络流量,角发送所有模板给客户不管在 NG-如果条件满足的。

I am using Directives to separate my code into templates, and I want those templates to load based on an if condition. Right now, when I look into the network traffic, Angular is sending all of the templates to the client regardless of if the ng-if condition is met.

<div class="container">
    <ul>
        <template1 ng-if="taskCategory == 'condition1'"></template1>
        <template2 ng-if="taskCategory == 'condition2'"></template2>
        <template3 ng-if="taskCategory == 'condition3'"></template3>
    </ul>
</div>

下面是我的指令模板的一个例子:

Here is an example of one of my directive templates:

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

这是预期的行为?预期在视觉上工作。但我是用的IM pression在数据将根据条件延迟加载,如果NG-。还是我误解了如何使用 NG-如果

Is this expected behavior? It is working visually as expected. But I was under the impression with ng-if data would lazy load based on the condition. Or am I misunderstanding how to use ng-if?

推荐答案

这绝对是预期的行为。 NG-如果将有条件地基于该条件的DOM删除元素。元素仍然被删除(否则元素的克隆重新添加到DOM)之前渲染。

That is definitely expected behavior. ng-if will conditionally remove elements from the DOM based on the condition. The element is still rendered prior to being removed (otherwise a clone of the element is added back into the DOM).

该行为是在 AngularJS第一款规定:ngif 文档

这篇关于AngularJS:指令模板无论装载NG-如果条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆