bindInput allows you to automatically bind a form input/control to a given
property (i.e. two-way binding).
class MyElement extends LitElement {
@property({type: String})
public name: string = 'Bob';
render() {
return html`
<label>
Name:
<input type="text" ${bindInput(this, 'name')}>
</label>
`;
}
}This will automatically two-way bind the input element and the name
property.
The parameters (bindInput(host, property)) are as follows:
host- any object which has the specifiedpropertyproperty- the name of the property onhostto bind
You may also use this directive with an attribute or property binding:
return html`
<label>
Name:
<input type="text" .value=${bindInput(this, 'name')}>
</label>
`;This may help with SSR, and will work the same way as the element binding usage.
The following elements are supported by this directive:
inputselecttextarea- Compatible elements
By default, we listen for the change event and the input event.
Checkbox inputs will result in a boolean value:
return html`
<input type="checkbox" ${bindInput(this, 'isEnabled')}>
`;In this example, this.isEnabled will be true or false depending on the
checked state of the input.
Select element bindings differ depending on if the select is multiple or not.
With <select multiple>, the value will be an array of the selected values.
With a regular <select>, the value will be the selected value.
Any element which implements the following will be supported:
- Emits
changeevents and/orinputevents - Has a
valueproperty
N/A