angular
angular copied to clipboard
Remove *ngIf="null" support/hack
It would be very nice to remove support for if (null), since this isn't supported in Dart:
https://github.com/dart-lang/angular/blob/c08a1bec6410357e45f4097a2210be370ce0ec2b/angular/lib/src/compiler/view_compiler/property_binder.dart#L673-L676
I imagine this a breaking change for our users.
My understanding was that we supported null values for when an input hadn't been set yet. Otherwise, the templates might throw some errors before the first change detection cycle even completed.
Though, this might be a tie-over from Angular1...
It used to be the case that every input was initialized with a null value if it wasn't set, but now the behavior for inputs is that the initial value is implicitly null, and so the first value to be set for an input must be non-null.
From an internal thread:
There are a few root causes here. Some easy to fix, some harder:
- Cases where a 'bool' state is uninitialized in the component:
class ExampleComp {
@Input()
bool showSomething;
}
... easily change-able to:
class ExampleComp {
@Input()
bool showSomething = false;
}
Unless the user was explicitly checking for == null, this change is a no-op and fixes the code.
- Cases where the user was using something like
foo?.isBar
Easily changeable to foo?.isBar == true
- Cases where the user was using a model object, and wrote
*ngIf="model.isBar"
This is actually the hardest one, since it looks common that the model is mocked out (via Mockito), but the property is never explicitly initialized (the default for Mockito is unfortunately "null" instead of throw).
Options
-
Do nothing. Revert and don't try to change this anytime soon.
-
Churn through the internal failures and fix . This might involve only fixing obvious things, and not cases where a Mockito mock returned null instead of false (this is harder to do and sometimes requires more domain knowledge).
-
Go the flag route. We could add this:
class NgIf {
static bool enableStrictBool = false;
}
... and allow teams to flip to NgIf.enableStrictBool = true.
This would also mean we could add this to main()s that are failing at the above CL, then flip the default, and then work on removing these flag instances. I don't know if we want to do this right now, though, and we could always do this post 5.0 as well.