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 specifiedproperty
property
- the name of the property onhost
to 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:
input
select
textarea
- 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
change
events and/orinput
events - Has a
value
property
N/A