[转]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=& ...
随机推荐
- ubuntu16.04 下安装 visual studio code 以及利用 g++ 运行 c++程序
参考链接:1. http://www.linuxidc.com/Linux/2016-07/132798.htm(安装vs code) 2.https://blog.csdn.net/qq_28598 ...
- 主引导扇区MBR的解析
http://blog.chinaunix.net/uid-24774106-id-3340397.html 最近排查一个USB相关的故障,由于信息安全就不多说工作上的事情了,顺路学习了MBR的相关知 ...
- 连接hive
bin/hiveserver2 nohup bin/hiveserver2 1>/var/log/hiveserver.log 2>/var/log/hiveserver.err & ...
- Centos 7 搭建.net web项目
现在的.NET Core 1.0版本是一个很小的核心,APIs和工具也并不完整,但是随着.Net Core的不断完善,补充的Apis和创新也会一起整合到.NET Framework中. 安装cento ...
- vue.js项目nginx部署
开发环境搭建完成.二.编译部署1.项目路径下demo输入命令npm run build编译完成后会发现在demo文件夹下多出一个dist文件夹这里面就是编译好的文件了.2.网上下载nginx,下载地址 ...
- 带权单源最短路发[稠密图](Dijkstra)
对于稠密图,采用邻接矩阵较为合适 所以我们先构建一个邻接矩阵 typedef int Vertex; typedef int WeightType; //图 typedef struct MyGrap ...
- yum-Remi源配置
Remi repository 是包含最新版本 PHP 和 MySQL 包的 Linux 源,由 Remi 提供维护. 有个这个源之后,使用 YUM 安装或更新 PHP.MySQL.phpMyAdmi ...
- LINK1104:无法打开文件"\.obj"原因及解决办法
那是由于具有空文件名的库导致的,只需要检查一下配置信息把出现空文件名的地方进行改正就可以了. 作者:耑新新,发布于 博客园 转载请注明出处,欢迎邮件交流:zhuanxinxin@foxmail.co ...
- 移动端IM开发者必读(一):通俗易懂,理解移动网络的“弱”和“慢”
本系列文章引用了腾讯技术专家樊华恒<海量之道系列文章之弱联网优化>的部分章节,感谢原作者. 1.前言 随着移动互联网的高速发展,移动端IM以移动网络作为物理通信载体早已深入人心,这其中的成 ...
- 学爬虫,需要掌握哪些Python基础?
入手爬虫确实不要求你精通Python编程,但基础知识还是不能忽视的,那么我们需要哪些Python基础呢? 首先我们先来看看一个最简单的爬虫流程: 第一步要确定爬取页面的链接,由于我们通常爬取的内容 ...