Guide: Form Input Bindings Flashcards
What elements does v-model
work on and what does it do?
-
input
,textarea
, andselect
elements - It automatically picks the correct way to update the element based on the input type
- Basically syntactic sugar for updating data on user input events
What are two importnat gotchas of v-model
?
- It will ignore initial
value
,checked
, orselected
attributes found on any form element that it’s on. It will treat the Vue instance data as the source of truth -
v-model
does trigger updates during IME composition (languages such as Chinese, Korean, Japanese,…)
How do you use v-model
for normal text?
`<input></input>
How do you use v-model
with multiline text?
And how do you get newlines to display properly?
And what’s something important to note about this element?
textarea v-model="message"\>
Use white-space: pre-line
on the the block containing the text.
Interpolation on textareas won’t work textarea\>{{text}}
: must use v-model
How do you use v-model with a single checkbox?
Use a boolean property and assign it to the checkbox. It will be either true
or false
<input></input>
How do you use v-model
with multiple checkboxes?
And what will happen?
<input type="checkbox" value="Jack" v-model="checkedNames">
<input type="checkbox" value="John" v-model="checkedNames">
Bounding them all to the same array will add and remove their values from the array
How do you use v-model
on a radio element?
<input type="radio" value="Blue" v-model="picked">
It will assign the value
of the radio element selected to the bound property.
How do you handle a single select with v-model
?
What’s an important gotcha?
<select v-model="selected">
</select>
It will assign the selected option to the bound property
You should always recommended to provided a disabled option with an empty value, because iOS will not fire the first item because it does not provide a not selected option if none of the options match the initial value.
How do you handle multiple select with v-model
?
If select
has the multiple
attribute, you must bind an array to the v-model
directive, in which case all options (or values) that are selected will be in the array.
Describe using bound values for input elements instead of static ones?
- Checkboxes
- Can use
true-value
andfalse-value
as values that will be assigned to the property inv-model
when the checkbox is and is not checked respectively
- Can use
- Radio
- use
:value
to assign a dynamic property to a radio element
- use
- Select
- again use
:value
to assign a dynamic property to an<option>
</option>
- again use
What’s the default behavior of v-model
syncing? And what modifier can be used to change that?
-
v-model
by default syncs after eachinput
event - Use the
.lazy
modifier to sync afterchange
events instead
What if you want to deal in numbers only with an input?
- Use the
.number
modifier to automatically typecast the value as a number - Even with
type="number"
, the element still returns a string - If value can’t be parsed with
parseFloat()
, then original value is returned
How do you handle trimming?
- Add
.trim
modifier tov-model
to automatically trim input