Angular 2 (+Dart) 内联条件:如果第一个条件为假,是否评估第二个条件?怎么解决?

Angular 2 (+Dart) inline conditionals: Is the second condition evaluated if the first is false? How to solve?

这是我的 component.html(Angular Dart 模板)中的代码:

<div *ngIf="selectedOffer!=null && !selectedOffer.isBotenOffer">...</div>

这是我得到的异常:

EXCEPTION: NoSuchMethodError: The getter 'isBotenOffer' was called on null. Receiver: null Tried calling: isBotenOffer

selectedOffer 我有时 null。但是,如果第一个语句为假,为什么要对第二个语句进行评估?我怎样才能最好地解决这个问题?

不,&& 当第一部分是 false 时,短路评估停止评估第二部分 - 与正常的 Dart 代码相同。

Angular 将其写入生成的模板文件:

_NgIf_0_5.ngIf = ((ctx.selectedOffer != null) && !ctx.selectedOffer.isBotenOffer);

所以如果 selectedOffernull,它不应该评估 selectedOffer.isBotenOffer。但是,您可能遇到了一个刚刚在 dart2js 中修复的错误:https://github.com/dart-lang/sdk/commit/fe7baee84828e109a49920c2572f3917e5ff8ca5

如果您在组件的构造函数中设置 selectedOffer,但在构造函数中提前 return,则可能会遇到此问题。