[转]Angular 4 *ngIf/Else
本文转自:http://tylerscode.com/2017/03/angular-4-ngifelse/
As you may know it wasn’t that many months ago that Angular 2 left RC and went Full Release(back in August). We are already upon the next big release of Angular with v4. Angular 4.0.0-rc.1 was released in late February with rc.2 hot on it’s heels 6 days later, today, March 2nd. There are lots of improvements including smaller bundle sizes and faster compilation. My favorite new feature at the moment is the new NgIf/Else syntax.
Previously, you may have used something like this:
|
1
2
3
4
5
6
7
|
<div *ngIf="someCondition"> <h1>Condition Passed!</h1></div><div *ngIf="!someCondition"> <h1>Condition Failed!</h1></div> |
Now you can use syntax like this:
|
1
2
3
4
5
6
7
|
<div *ngIf="someCondition; else falsyTemplate"> <h1>Condition Passed!</h1></div><ng-template #falsyTemplate> <h1>Condition Failed!</h1></ng-template> |
You can specify another template using ng-template, give it a variable using # and then reference it in the *ngIf statement with an else clause.
You can also use a more explicit syntax with NgIf/Else/Then. It would look something like this:
|
1
2
3
4
5
6
7
8
9
|
<div *ngIf="someCondition; then truthyTemplate else falsyTemplate"></div><ng-template #truthyTemplate > <h1>Condition Passed!</h1></ng-template><ng-template #falsyTemplate> <h1>Condition Failed!</h1></ng-template> |
In my opinion this helps code readability as it makes it more explicit and easier to follow. No more falsy checks with !someCondition like code.
Also, the async pipe was added to *ngIf. Previously you may have had a form or page that contained several fields that all independently subscribed to observables using the async pipe. It may have looked something like this:
|
1
2
3
|
<p>{{someObservableOne | async}}</p><p>{{someObservableTwo | async}}</p><p>{{someObservableThree | async}}</p> |
Now you can wrap all those observables into a single observable and subscribe to it in the *ngIfstatement and assign a local object variable to reference in all your fields like this:
|
1
2
3
4
5
6
7
|
<div *ngIf="someObservable | async; else loadingScreen; let myObject"> <p>{{myObject.propertyOne}}</p> <p>{{myObject.propertyTwo}}</p> <p>{{myObject.propertyThree}}</p></div><ng-template #loadingScreen>loading...</ng-template> |
This code, in my opinion, is cleaner because it only subscribes to a single observable once to retrieve data. I hope this feature is as beneficial to others as it is to me.
[转]Angular 4 *ngIf/Else的更多相关文章
- angular 中*ngIf 和*ngSwitch判断语句
<div style="text-align:center"> <h1> Welcome to {{ title }}! </h1> <p ...
- [Angular] Show a loading indicator in Angular using *ngIf/else, the as keyword and the async pipe
The network may be unreliable and loading data may take time. Thus it is important to give the user ...
- angular源码分析:angular中jqLite的实现——你可以丢掉jQuery了
一.从function JQLite(element)函数开始. function JQLite(element) { if (element instanceof JQLite) { //情况1 r ...
- Angular 显示英雄列表
在本页面,你将扩展<英雄指南>应用,让它显示一个英雄列表, 并允许用户选择一个英雄,查看该英雄的详细信息. 创建模拟(mock)英雄数据 你需要一些英雄数据以供显示. 最终,你会从远端的数 ...
- angular官网实例(综合)
第一部分: (应用的“外壳”) 1.新建项目: ng new mytest 2.进入项目目录,并启动这个应用. cd mytest ng serve --open 3.添加一个标题 打开 app.co ...
- AngularJS进阶(三十八)上拉加载问题解决方法
AngularJS上拉加载问题解决方法 项目中始终存在一个问题:当在搜索栏输入关键词后(见图1),按照既定的业务逻辑应该是服务端接收到请求后,首先返回查询的前7条数据,待客户端出现上拉加载时,继续查找 ...
- Angular6 学习笔记——指令
angular6.x系列的学习笔记记录,仍在不断完善中,学习地址: https://www.angular.cn/guide/template-syntax http://www.ngfans.net ...
- Ionic Js十六:滚动条
ion-scroll ion-scroll 用于创建一个可滚动的容器. <ion-scroll [delegate-handle=""] [direction="& ...
- ionic基础知识
ion-header-bar(头部 页眉) 在内容顶部添加一个固定header栏. 用法 <ion-header-bar align-title="left" class=& ...
随机推荐
- deug的使用经验
最基本的操作是: 1, 首先在一个java文件中设断点,然后运行,当程序走到断点处就会转到debug视图下, 2, F5键与F6键均为单步调试,F5是step into,也就是进入本行代码中执行,F6 ...
- 1.2 eigen中矩阵和向量的运算
1.2 矩阵和向量的运算 1.介绍 eigen给矩阵和向量的算术运算提供重载的c++算术运算符例如+,-,*或这一些点乘dot(),叉乘cross()等等.对于矩阵类(矩阵和向量,之后统称为矩阵 类) ...
- ABP框架系列之四十六:(Setting-Management-设置管理)
Introduction Every application need to store some settings and use these settings in somewhere in th ...
- C++与C语言在结构体上的区别
用Nios 实现逻辑上很清楚,只是C++用switch语句后,写的很麻烦,主要是Switch语句很长吧. 另外要记录下:struct在C++中,在a文件中定义在b文件中定义变量是可以的,但在C语言中, ...
- Scanner 类的使用
/* Scanner类的使用: 1.导类 2.创建对象 3.调用对应的方法获取数据 */ import java.util.Scanner;//导入Scanner类 public class IfDe ...
- ubuntu下chrome浏览器安装flash插件(pepperflashplugin-nonfree)
安装前说明: ubuntu的Google 已经不能使用Adobe Flash了,需要用PepperFlashPlayer来替代 Adobe Flash才行. 安装步骤: 1.安装pepperflash ...
- DevExpress--TreeList节点添加图片
这个过程相对来说比较简单,网上也有不少资料,但是自己在做过之后为了记住,算是给自己写一个博客吧. 下面直接上具体的流程 1.前提 控件使用的都是DevExpress和winform的原生控件两种: 2 ...
- Codeforces gym101612 E.Equal Numbers(贪心)
传送:http://codeforces.com/gym/101612 题意:给出一个大小为n的序列a[i],每次选其中一个数乘以一个正整数,问进行k步操作后最少剩下多少种数字,输出0≤k≤n,所有的 ...
- 动态页面技术EL
1.EL概述: EL表达式可以嵌入再JSP页面内部,减少JSP脚本的编写 EL出现的目的是要替代JSP页面中脚本的编写 EL最重要的作用就是从域中取出数据: 引入: JSP中<%=request ...
- LabVIEW(十四):VI属性
1.VI的属性在项目研究中是很常用的,但是会经常忽略VI的属性设置,建议在编程事常常运用这些属性的设置,这样就可以使你的程序更加完善,易懂性也会提高. 属性快捷键:Ctrl+I. 2.常规 (1).编 ...