在组合这些ng指令写到一篇文章里的时候,基本是有规则的,本兽会将功能相似相近的一类整合到一篇文章,方便理解和记忆。

这篇的三个指令也都是对DOM元素的操作,页面上显示/隐藏的判断,添加/移除的判断。

ngIf

ngIf指令会根据指定的表达式返回的boolean类型值对该元素做添加到/移除出Dom树的操作。

格式:ng-if=“value”

value:表达式  返回结果为boolean类型。

使用代码:

  <input type="button" value="show/hide" ng-click="show = !show;" />
<div ng-if="show">Hello World</div>

这里做了个对Hello World 所在的div的显示/隐藏效果。但是如果你打开浏览器开发工具去审查元素,会发现这个div是被移除和添加的,只留下一个注释“<!-- ngIf: show -->”在div所在的地方。这个需要和下面的ngShow/ngHide做个区分。也就是说这个指令对DOM的操作是移除出/添加进DOM树了。

ngSwitch

ngSwitch指令可以根据表达式的结果在模板上按条件切换DOM结构。元素内使用ngSwitch而没使用ngSwitchWhen或者ngSwitchDefault指令的将会被保存在模板中。

格式:ng-switch  on=“expression” ng-switch-default  ng-switch-when=“value”

expression: 表达式,返回判断的条件是否成立的boolean值。

value:设置的条件

使用代码:

  <div ng-app="Demo" ng-controller="testCtrl as ctrl">
<select ng-model="ctrl.tpl" ng-options="i for i in ctrl.select">
<option value="0">请选择模板</option>
</select>
<div ng-switch on="ctrl.tpl">
<p ng-switch-default>tpl-one</p>
<p ng-switch-when="tpl-two">tpl-two</p>
<p ng-switch-when="tpl-three">tpl-three</p>
</div>
</div>
  (function () {
angular.module("Demo", [])
.controller("testCtrl", testCtrl);
function testCtrl() {
this.select = ["tpl-one", "tpl-two","tpl-three" ];
this.tpl = this.select[0];
};
}());

ngSwitch根据表达式的成立与否添加对应的内容到写好的HTML位置,而这个位置当表达式为false时也是个注释就像:“<!-- ngSwitchWhen: tpl-two -->”。这是第二个模板绑定的位置,当表达式满足第二个模板的条件,那么这里就会被第二个模板的HTML代替并显示到页面。

ngHide/ngShow

NgHide/ngShow指令显示或隐藏指定的THML元素。元素的显示隐藏是根据元素上ng-hide的css样式添加删除实现的。

格式:ng-hide=”value”   ng-show=”value”

value:表达式 结果为boolean类型。

使用代码:

   <input type="button" value="show/hide" ng-click="show = !show;" />
<div ng-show="show">Hello</div>
<div ng-hide="show">World</div>

ngShow和ngHide相反。这里把ng-hide的css样式写在下面吧:

   .ng-hide {
/* this is just another form of hiding an element */
display: block!important;
position: absolute;
top: -9999px;
left: -9999px;
}

最新文章

  1. Hexo的Next主题配置
  2. [Asp.net mvc]jquery.form.js无刷新上传
  3. Html - 对话箭头
  4. C51 的编程规范
  5. Cocoa框架
  6. 使用CSS3动画实现绚丽的照片墙效果
  7. css的各种选择器
  8. Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, given n = 3, a solution set is: &quot;((()))&quot;, &quot;(()())&quot;, &quot;(())()&quot;, &quot;()(())&quot;, &quot;()()()&quot;
  9. 《Java从入门到放弃》JavaSE入门篇:面向对象概念(入门版)
  10. Webpack的加载器
  11. .gvfs: Permission denied
  12. react中使用antd遇到的问题
  13. [Oracle][PDB]PDB restore/recover 过程记录
  14. SQLite中的WHERE子句
  15. 《图说VR入门》——DeepoonVR的大鹏(陀螺仪)枪
  16. Xcode 6 下添加pch头文件
  17. 【Android】Android版本和API Level对应关系
  18. win8 学习笔记二 输出日志
  19. poj 1811 Prime Test 大数素数测试+大数因子分解
  20. 图像GIF格式介绍

热门文章

  1. 高性能JavaScript 循环语句和流程控制
  2. Node 进阶:express 默认日志组件 morgan 从入门使用到源码剖析
  3. Android Intent的几种用法全面总结
  4. Competition-based User Expertise Score Estimation-20160520
  5. SQL中的内连接与外连接
  6. MVC———用自定义扩展类实现验证
  7. android Baseadapter 和 ViewHolder的使用
  8. 延时程序执行Qt
  9. BIM软件小技巧:Revit2014所有快捷键汇总表格
  10. [转] 使用Spring MVC构建REST风格WEB应用