javascript - ng-if ="true"+ data-ng-if ="true"计算结果为 false?

标签 javascript angularjs

事实是,如果您在 htmlng-if="truthyValue" 和 data-ng-if="truthyValue" > 元素使用 angularJS +1.6,受影响的元素将不会在 DOM 中呈现。即使您将 ng-ifdata-ng-ifboolean true 一起使用也是如此。


有人知道为什么会这样吗?

最佳答案

这个答案需要多次引用 AngularJS 的 directive documentation .


这是因为 AngularJS HTML 模板是编译的:

For AngularJS, "compilation" means attaching directives to the HTML to make it interactive.

在编译期间,模板指令会经历一个称为规范化的过程,该过程涉及将不区分大小写的 HTML 名称更改为 AngularJS 中使用的驼峰命名法,这遵循以下过程:

  1. Strip x- and data- from the front of the element/attributes.
  2. Convert the :, -, or _-delimited name to camelCase.

在这个过程之后,您可以看到您将两个 ngIf 指令附加到同一个 HTML 元素,然后这些指令会干扰(它对两次出现的ng-if 而不是 ng-ifdata-ng-if 是您的网络浏览器丢弃了其中一个 ng -ifs 如果有两个而不是它们都到达 AngularJS 编译器)。


因此,避免这种情况发生的解决方案是不要在 AngularJS 规范化为同一事物的 HTML 中使用多个指令

关于javascript - ng-if ="true"+ data-ng-if ="true"计算结果为 false?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46098315/

相关文章:

javascript - 自定义过滤器在 Angular Hybrid 应用程序中不起作用

javascript - 在 Ajax 加载时滚动到项目底部

javascript - 将每个循环变量传递给 Javascript 函数

JavaScript 或 Node.js 意外地对对象进行排序

javascript - 无法在数组上进行双向数据绑定(bind)

unit-testing - 嵌套 Controller 的单元测试

javascript - 我在 IE8 中不断收到 Object.create 错误

javascript - 当范围更改时,如何让 ng-select 进行更新?

javascript - 空格键从 javascript 检索模板

javascript - jQuery:用动态 HTML 替换 HTML,而不是附加