[转]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=& ...
随机推荐
- 词向量之word2vec实践
首先感谢无私分享的各位大神,文中很多内容多有借鉴之处.本次将自己的实验过程记录,希望能帮助有需要的同学. 一.从下载数据开始 现在的中文语料库不是特别丰富,我在之前的文章中略有整理,有兴趣的可以看看. ...
- Myeclipse中的tomcat项目的内存溢出
tomcat中 内存溢出 在这里写上 -Xmx1024M -Xms1024M -XX:NewSize=128m -XX:MaxNewSize=128m -XX:PermSize=128m -XX:Ma ...
- @Scheduler与cron
- UVa 11645 Bits (暴力+组合数学)
题意:给定一个数 n,求 0 ~ n,中二进制表示中连续两个 1 出现的次数. 析:枚举连续的两个 1,从低位向高位进行枚举,然后前可以是任意数,后面也是任意的,如果 n 正好是 11 还要另算,举个 ...
- 参数签名ascii码排序的坑
参数签名中通常是按键值对中键名称的ASCII按从小到大的顺序排序后进行hash为签名字符串.不要直接使用 SortedDictionary<string, string> 有坑的,他是按数 ...
- MySQL优化--NOT EXISTS和LEFT JOIN方式差异
在MySQL中,我们可以将NOT EXISTS语句转换为LEFT JOIN语句来进行优化,哪为什么会有性能提升呢? 使用NOT EXISTS方式SQL为: ) FROM t_monitor m WHE ...
- 【BZOJ3676】 [Apio2014]回文串(SAM,manacher)
传送门 BZOJ 洛谷 Solution 考虑我们每找到一个回文串就更新一次答案,跑个SAM,这样子复杂度是爆炸的. 接下来的就是优化: 我们可以倍增跳直到跳不了,最后的siz就是出现次数. 没了?没 ...
- JQuery Mobile - 固定住页面和页脚
在点击页面空白时候,页眉和页脚会隐藏,在页眉和页脚加入以下代码就可以了: data-tap-toggle ="false" 例子: <div data-role=" ...
- 人工智能必须要知道的语义分割模型:DeepLabv3+
图像分割是计算机视觉中除了分类和检测外的另一项基本任务,它意味着要将图片根据内容分割成不同的块.相比图像分类和检测,分割是一项更精细的工作,因为需要对每个像素点分类,如下图的街景分割,由于对每个像素点 ...
- Mac 安装微软雅黑字体
https://www.jianshu.com/p/d8c34fff3483 1.找一台Windows电脑,打开字体文件夹C:\Windows\Fonts. 2.搜索"Calibri.微软雅 ...