Introduce a way for a component to require an attribute #16126
Description
I'm submitting a ...
- bug report
- feature request
- other (Please do not submit support requests here (see above))
Problem description:
Let's have a component (for simpleButton
) with following bindings:
bindings: {
name: '@',
enabled: '<',
onClick: '&',
},
And it needs to be used with all the bindings set like this <simple-button enabled="true" name="Foo" onClick="foo()"></simple-button>
. A programmer shouldn't be able to use it without one or more non-optional attribute like this <simple-button></simple-button>
or <simple-button enabled="true" name="Foo"></simple-button>
.
There's no way to ensure that all required bindings will be set. This causes unpredictable behavior and it's a bit hard to debug if any of them is forgotten.
https://plnkr.co/edit/OwrnxuU3QhsvyNBMSrXh?p=preview
Current behavior:
There's no way to enforce that a binding in the component is required and must be set as an attribute.
If a binding is non-optional (without ?
) and no value is given it's set to undefined
.
At the moment <simple-button enabled="undefined"></simple-button>
and <simple-button></simple-button>
are indistinguishable from each other (when non-optional).
Expected / new behavior:
It throws an error if required binding is not set as an attribute of a component.
Angular version:
latest
Anything else:
I see two possible ways to solve this.
The first is to enforce all bindings without ?
to be set as an attribute of a component. But I can see that may be a bit too radical. I tried and it worked but broke 105 tests :/
The second is to introduce a new marker that will indicate that attribute must be set. !
looks like a good candidate.
I'm willing to submit a PR if there's a decision how to proceed :)