Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Force v-model to update its value? #1136

Closed
patrick-hertling opened this issue Aug 9, 2015 · 10 comments
Closed

Force v-model to update its value? #1136

patrick-hertling opened this issue Aug 9, 2015 · 10 comments

Comments

@patrick-hertling
Copy link

Sometimes you have a textfield set up as v-model which can change due to an external javascript process(like a datepicker).
In those cases, the 2-way-binding of the v-model doesn't gets fired. Is there a way of forcing Vue to fetch the actual value of his binding(In case it had changed and Vue didn't notice).

@simplesmiler
Copy link
Member

@patrick-hertling I don't think this should be solved by vue, but rather by the external library you are using. With jQuery-stuff you should $(element).trigger('change') or $(element).trigger('input'), depending on your needs. In general case, custom two-way directives are the solution you might be looking for.

@patrick-hertling
Copy link
Author

Thank you. I think I will use a directive.

@prakashgp
Copy link

prakashgp commented Mar 9, 2017

How do we use two way directives ? i tried, using @change="(value) => checked = value" to detect change and set the value, but it is not working for me

@markskayff
Copy link

I'm having this issue while testing a Vue controlled form with Selenium. When running the Selenium tests Vue doesn't catch the changes. I wonder if there is a method to refresh the whole dataset from the view.

@prakashgp
Copy link

@markskayff i found this way:
for text inputs, dispatch native input event, for other types use native change events to trigger v model update

@sudhir600
Copy link

I am facing the same issue.
is there any working solution?

@eddyerburgh
Copy link
Member

eddyerburgh commented May 9, 2018

If you set properties on an element imperatively, you need to dispatch a native event to force bound data to update:

const event = new Event('input')
vm.$refs.child.value = 'something'
vm.$refs.child.dispatchEvent(event)

Different elements need different event types:

element event type property
<input[type="checkbox"]> change checked
<input[type="radio"]> change checked
<input> input value
<textarea> input value
<option> change selected

@sudhir600
Copy link

sudhir600 commented May 9, 2018

@eddyerburgh, I tried your solution but it's not working in my scenario.
Pls see this completed details about my scenario - https://nuxtjs.cmty.io/nuxt/nuxt.js/issues/c7037

i am getting this error-
Index.vue?4344:148 Uncaught TypeError: this.cartForm.item[n].dispatchEvent is not a function

@EVINK
Copy link

EVINK commented Apr 4, 2020

@eddyerburgh Thank you, your way is a good solution to Input element, but it seems not working with the image element.

I got some images they bind to different data, but they also bind to the same function. And I do not want to bind value using if-else branch。

<img :src="d.A" @click="fn" />
...
<img :src="d.Z" @click="fn" />

fn(e) {
    //  this is it should be
    e.target.src = 'some-address'
    
    //  this is just not good
    if(xxxx) d.A = 'xxxx'
    ...
    else d.Z = 'xxxx'
}

@thegitdude
Copy link

thegitdude commented Jun 21, 2020

Wrap the native textField component in a custom component, add a watcher for the changing property, in the watcher call the forceUpdate() function to re-render the component.

TS texample:

    @Watch('value')
    private watchValue(val: string, oldVal: string) {
        this.time = val
        this.$forceUpdate()
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants