本文转自: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的更多相关文章

  1. angular 中*ngIf 和*ngSwitch判断语句

    <div style="text-align:center"> <h1> Welcome to {{ title }}! </h1> <p ...

  2. [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 ...

  3. angular源码分析:angular中jqLite的实现——你可以丢掉jQuery了

    一.从function JQLite(element)函数开始. function JQLite(element) { if (element instanceof JQLite) { //情况1 r ...

  4. Angular 显示英雄列表

    在本页面,你将扩展<英雄指南>应用,让它显示一个英雄列表, 并允许用户选择一个英雄,查看该英雄的详细信息. 创建模拟(mock)英雄数据 你需要一些英雄数据以供显示. 最终,你会从远端的数 ...

  5. angular官网实例(综合)

    第一部分: (应用的“外壳”) 1.新建项目: ng new mytest 2.进入项目目录,并启动这个应用. cd mytest ng serve --open 3.添加一个标题 打开 app.co ...

  6. AngularJS进阶(三十八)上拉加载问题解决方法

    AngularJS上拉加载问题解决方法 项目中始终存在一个问题:当在搜索栏输入关键词后(见图1),按照既定的业务逻辑应该是服务端接收到请求后,首先返回查询的前7条数据,待客户端出现上拉加载时,继续查找 ...

  7. Angular6 学习笔记——指令

    angular6.x系列的学习笔记记录,仍在不断完善中,学习地址: https://www.angular.cn/guide/template-syntax http://www.ngfans.net ...

  8. Ionic Js十六:滚动条

    ion-scroll ion-scroll 用于创建一个可滚动的容器. <ion-scroll [delegate-handle=""] [direction="& ...

  9. ionic基础知识

    ion-header-bar(头部 页眉) 在内容顶部添加一个固定header栏. 用法 <ion-header-bar align-title="left" class=& ...

随机推荐

  1. 获取IP地址方法

    function getip() {     static $ip = '';     $ip = $_SERVER['REMOTE_ADDR'];     if(isset($_SERVER['HT ...

  2. Linux 第十天

    十三.权限管理 1.ACL权限开启 1)dumpe2fs -h /dev/sda3查看分区ACL权限是否开启 -h:仅显示超级块中信息,而不显示磁盘块组的详细信息 2)mount -o remount ...

  3. Linux 第七天

    软件包管理 1.软件包分类 1)源码包(脚本安装包) 优点: l  开源,如果有足够的能力,可以修改源代码 l  可以自由选择所需的功能 l  软件是编译安装,所以更加适合自己的系统,更加稳定也效率更 ...

  4. ABP框架系列之五十二:(Validating-Data-Transfer-Objects-验证数据传输对象)

    Introduction to validation Inputs of an application should be validated first. This input can be sen ...

  5. linux上安装zookeeper

    本文先讲述Linux下单机版的安装流程,集群的配置后续再补上.关于Zookeeper的基本介绍和原来在本文不做更多介绍,可以自行查找.本文的操作流程相对简单,仅做备忘而已. 第一步  安装JDK:   ...

  6. 深度学习框架caffe/CNTK/Tensorflow/Theano/Torch的对比

    在单GPU下,所有这些工具集都调用cuDNN,因此只要外层的计算或者内存分配差异不大其性能表现都差不多. Caffe: 1)主流工业级深度学习工具,具有出色的卷积神经网络实现.在计算机视觉领域Caff ...

  7. samba服务配置(一)

    samba是一个实现不同操作系统之间文件共享和打印机共享的一种SMB协议的免费软件. samba软件结构: /etc/samba/smb.conf    #samba服务的主要配置文件 /etc/sa ...

  8. Golang 调用 Python 代码

    go 中的 cgo 模块可以让 go 无缝调用 c 或者 c++ 的代码,而 python 本身就是个 c 库,自然也可以由 cgo 直接调用,前提是指定正确的编译条件,如 Python.h 头文件( ...

  9. Internetworking

    1 Introduction 所谓的InternetWorking就是将很多网络连接起来,那么在这种连接的网络下我们该如何传送封包呢? 2 IP and Routers 1 IP Datagram H ...

  10. faster-RCNN台标检测

    最近学习了faster-RCNN算法,收获不少,记此文为证.faster-RCNN是一个目标检测算法,它能够识别多个目标,对目标分类并标注位置,非常好用.它的输入样本是标注好的图片,输出是一个hdf5 ...