You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
You can use the `v-model` directive to create two-way data bindings on form input elements and elements with `contenteditable` attribute. It automatically picks the correct way to update the element based on the input type.
6
+
## The Basics
7
+
8
+
You can use the `v-model` directive to create two-way data bindings on form input elements. It automatically picks the correct way to update the element based on the input type.
When you need to dynamically render a list of options for a `<select>` element, it's recommended to use an `options` attribute together with `v-model`:
In your data, `myOptions` should be an keypath/expression that points to an Array to use as its options. The Array can contain plain strings, or contain objects.
89
+
90
+
The object can be in the format of `{text:'', value:''}`. This allows you to have the option text displayed differently from its underlying value:
91
+
92
+
```js
93
+
[
94
+
{ text:'A', value:'a' },
95
+
{ text:'B', value:'b' }
96
+
]
97
+
```
98
+
99
+
Will render:
100
+
101
+
```html
102
+
<select>
103
+
<optionvalue="a">A</option>
104
+
<optionvalue="b">B</option>
105
+
</select>
106
+
```
107
+
108
+
Alternatively, the object can be in the format of `{ label:'', options:[...] }`. In this case it will be rendered as an `<optgroup>`:
109
+
110
+
```js
111
+
[
112
+
{ label:'A', options: ['a', 'b']},
113
+
{ label:'B', options: ['c', 'd']}
114
+
]
115
+
```
116
+
117
+
Will render:
118
+
119
+
```html
120
+
<select>
121
+
<optgrouplabel="A">
122
+
<optionvalue="a">a</option>
123
+
<optionvalue="b">b</option>
124
+
</optgroup>
125
+
<optgrouplabel="B">
126
+
<optionvalue="c">c</option>
127
+
<optionvalue="d">d</option>
128
+
</optgroup>
129
+
</select>
130
+
```
131
+
132
+
## Casting Value as Number
133
+
134
+
If you want user input to be automatically persisted as numbers, you can add a `number` attribute to your `v-model` managed inputs:
0 commit comments