Angularjs   发布时间:2022-04-20  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Angular 安全导航操作符(?.)和空属性路径大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

Angular 的安全导航操作符(?.)是一种流畅而便利的方式,用来保护出现属相路径中null和undefined值。

这种用法基本上和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简介

大佬总结

以上是大佬教程为你收集整理的Angular 安全导航操作符(?.)和空属性路径全部内容,希望文章能够帮你解决Angular 安全导航操作符(?.)和空属性路径所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。