Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Computed/Dynamic v-model name inside v-for

Tags:

vue.js

I have a form that's generated through a v-for.

Note: I'm using "@" to escape blade.

My vue isstance has:

data: {
    form: {
        inputs: [{icon: "",  name="",  placeholder: "", type="",  value=""}]
    },
    owner: {first_name: "", last_name: "", cell_phone: "", email: ""}
}

I generate the form with:

<template v-for="input in form.inputs">
    <div style="margin-bottom: 25px" class="input-group">
        <span class="input-group-addon"><i class="glyphicon glyphicon-@{{input.icon}}"></i></span>
         <input id="login-@{{input.name}}" v-model="?????" type="@{{input.type}}" class="form-control" name="@{{input.name}}" placeholder="@{{input.placeholder}}">
    </div>
</template>

I want to bind each input to their respective contractor property. So I want the v-models values to be owner.first_name, owner.last_name, owner.cell_phone, and owner. email. I was hoping I could do:

 v-model="'owner' + @{{input.name}}"

But I get:

[Vue warn]: v-model="'owner' + {{input.name}}": attribute interpolation is not allowed in Vue.js directives and special attributes.
vue.js:1956 Error: Warning Stack Trace
    at warn (vue.js:1956)
    at Directive.bind (vue.js:4507)
    at Directive._bind (vue.js:8668)
    at linkAndCapture (vue.js:7367)
    at compositeLinkFn (vue.js:7345)
    at new Fragment (vue.js:5373)
    at FragmentFactory.create (vue.js:5575)
    at Directive.create (vue.js:5844)
    at Directive.diff (vue.js:5757)
    at Directive.update (vue.js:5692)

The owner properties correspond to the input.name for each input.

Thanks

Description of the app: I am trying to build an owner by using multiple forms. Each form is generated through ajax request that gets a form object which contains the inputs n and the action for that form.

like image 203
Deciple Avatar asked Dec 13 '15 07:12

Deciple


1 Answers

You could try this:

v-model="owner[input.name]"
like image 97
Pantelis Peslis Avatar answered Oct 10 '22 04:10

Pantelis Peslis