本文转自: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. com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax;问题的解决

    哇,时隔两天时间,终于找到这个问题的解决办法,先看问题 这是我最近写的家庭记账本网页版,按顺序输入点击保存,总是弹出添加失败的提示 顺着找原因,把原因锁定在dao层的sql语句上,反复检查,没有找到一 ...

  2. Python开发——7.迭代器、生成器和装饰器

    一.迭代器 1.迭代器协议 (1)迭代器协议是指:对象必须提供一个next方法,执行该方法要么返回迭代中的下一项,要么引起一个StopIteration移除异常以中止迭代. (2)可迭代对象:实现了迭 ...

  3. 01-jQuery的介绍

    1.为什么要使用jQuery 在用js写代码时,会遇到一些问题: window.onload 事件有事件覆盖的问题,因此只能写一个事件. 代码容错性差. 浏览器兼容性问题. 书写很繁琐,代码量多. 代 ...

  4. 通过HPS控制FPGA的GPIO

    1.学习目的 本例程主要是让 SoC FPGA 初学者了解 HPS/ARM 如何跟 FPGA 交互.“My First HPS-FPGA”工程演示了实现方法的细节.这个工程包括 Quartus II ...

  5. 如何将uniurlframe中html调用delphi的函数

    uniGUI总群中台中cmj朋友为我们总结了如下内容,对于利用delphi+uniGUI开发应用,可以说是精品,必须掌握. 一句话,如何在html与delphi间交互代码,是最好的答案. [Clien ...

  6. Android-Java-构造方法内存图

    描述Dog对象: package android.java.oop07; // 描述Dog对象/实体 public class Dog { private String name; private i ...

  7. 分享Azure DevOps技术,来微信群吧!

    现在QQ用户越来越少,基本上都转移到微信上了. 讨论问题,动不动就来一个微信群.下面这样几百人的微信群,专门讨论Azure DevOps (TFS)技术,你加入了么? 还等什么,扫描吧!

  8. 机器翻译质量评测算法-BLEU

    机器翻译领域常使用BLEU对翻译质量进行测试评测.我们可以先看wiki上对BLEU的定义. BLEU (Bilingual Evaluation Understudy) is an algorithm ...

  9. centos7上安装cacti监控

     简介 在企业网络运维过程中,管理员必须时刻关注服务器的运行状态,如CPU.内存.磁盘空间使用情况等.为了能够及时的发现问题,尽量减少故障的发生.当网络中的设备,服务器等数量较多时,可以部署一套监控系 ...

  10. python中基于queue的打印机仿真算法

    使用打印机的模型是queue中最经典的应用之一,这里就回顾一下queue在这里的使用方法和 起的重要作用. 为了仿真打印状态,这里需要把真实环境中的三个物理模型要建模出来,分别是:打印者,打印 任务, ...