Angular 安全导航操作符(?.)和空属性路径
2017-12-05 15:14
162 查看
Angular 的安全导航操作符(?.)是一种流畅而便利的方式,用来保护出现属相路径中null和undefined值。
这种用法基本上和C#6.0中的委托触发操作符用法相同。
student?.name:当student不为空的时候输出name的值。
一、空属性路径
在Angular中 当前模块属性为空时,输出空字符串,不会抛出异常
二、空对象路径
在Angular中 对象为空时抛出异常
解决方案1:使用 && 逻辑判断符
解决方案2:使用 *ngIf 判断指令
解决方案3:使用?. 安全导航操作符(推荐)
更多:
Angular2开发环境搭建之VS Code
Angular CLI简介2
Angular CLI简介
这种用法基本上和C#6.0中的委托触发操作符用法相同。
student?.name:当student不为空的时候输出name的值。
一、空属性路径
在Angular中 当前模块属性为空时,输出空字符串,不会抛出异常
<div class="alert alert-info"> <p *ngIf="content">this is {{content}}</p> <p>测试内容</p> <p>this is {{content}}</p> </div>
import { Component } from '@angular/core'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent { title = 'app'; }
二、空对象路径
在Angular中 对象为空时抛出异常
解决方案1:使用 && 逻辑判断符
<p>对象为空:{{student&&student.name}}</p>
解决方案2:使用 *ngIf 判断指令
<p *ngIf="content">this is {{content}}</p>
解决方案3:使用?. 安全导航操作符(推荐)
<p>对象为空2:{{student?.name}}</p>
更多:
Angular2开发环境搭建之VS Code
Angular CLI简介2
Angular CLI简介
相关文章推荐
- Angular4_安全导航操作符 ( ?. ) 和空属性路径
- Angular中的安全导航操作符 ( ?. )
- Angular4图片上传预览路径不安全问题
- 一周乱弹(3-18 bootstrap模态框表单提交、jsp页面获取项目路径、bootstrap三级导航菜单、css是否显示属性)
- 使用反射依导航路径调用对象成员方法,或返回对象子、孙属性或字段成员[转]
- CodeFirst中导航属性的代码实现 理解
- Silverlight3 导航属性,Entity属性 的疑问
- 关系与导航属性
- 导航中路径规划模块与算法
- Angular企业级开发-AngularJS1.x学习路径
- 命名对象继承1-验证Create*命名对象安全属性的传递
- 《Entity Framework 6 Recipes》中文翻译系列 (29) ------ 第五章 加载实体和导航属性之过滤预先加载的实体集合和修改外键关联
- 导航和路径规划-论文心得
- C#加密路径里的参数,保护参数隐私安全
- AI-路径导航(最短路径算法 and A算法)
- C语言中为文件设置安全属性(WIN)
- EntityFramework4.5使用Expression类创建动态查询及动态查询导航属性
- VS2010项目属性的默认包含路径设置方法
- JavaScript根据指定路径查找对象属性值
- XP NTFS格式 右击文件夹属性没有安全选项的问题