这是我的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
。但是,如果第一个语句为假,为什么要对第二个语句进行评估?我怎样才能最好地解决这个问题?
最佳答案
Angular 会将其写入生成的模板文件中:
_NgIf_0_5.ngIf = ((ctx.selectedOffer != null) && !ctx.selectedOffer.isBotenOffer);
因此,如果 selectedOffer
为 null
,它不应评估 selectedOffer.isBotenOffer
。但是,您可能遇到了一个刚刚在 dart2js 中修复的错误:https://github.com/dart-lang/sdk/commit/fe7baee84828e109a49920c2572f3917e5ff8ca5
如果您在组件的构造函数中设置 selectedOffer
,但在构造函数中提前返回,则可能会遇到此问题。
关于Angular 2 (+Dart) inline conditionals : Is the second condition evaluated if the first is false? 如何解决?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43260815/