美文网首页我爱编程angular
angular--安全导航操作符 ( ?. )、非空断言操作符(

angular--安全导航操作符 ( ?. )、非空断言操作符(

作者: 林ze宏 | 来源:发表于2018-01-03 13:51 被阅读0次

安全导航操作符 ( ?. )

  • Angular 的安全导航操作符 (?.) 是一种流畅而便利的方式,用来保护出现在属性路径中 null 和 undefined 值。 下例中,当currentHero为空时,保护视图渲染器,让它免于失败。
The current hero's name is {{currentHero?.name}}

非空断言操作符(!)

  • 在 TypeScript 2.0 中,我们可以使用--strictNullChecks标志强制开启严格空值检查。TypeScript就会确保不存在意料之外的null或undefined。
  • 安全导航操作符不同的是,非空断言操作符不会防止出现null或undefined。 它只是告诉 TypeScript 的类型检查器对特定的属性表达式,不做 "严格空值检测"。
<!--No hero, no text -->
<div *ngIf="hero">
  The hero's name is {{hero!.name}}
</div>

参考:https://angular.cn/guide/template-syntax

相关文章

网友评论

    本文标题:angular--安全导航操作符 ( ?. )、非空断言操作符(

    本文链接:https://www.haomeiwen.com/subject/wmyhnxtx.html