Vue.js's two-way data binding feature is currently functioning in only one direction

Here is an example of my code:

<input type="text" v-model="selectedPost.title" v-bind:value="selectedPost.title">

and this is my Vue.js application:

var app = new Vue({
  el: '#app',
  data: { 
          selectedPost: {} 
        }
});

After typing in the input field, I can see the model being updated by checking the browser console:

app.$data.selectedPost.title

The value typed in the textbox is displayed correctly. However, when I try to manually update the value using:

app.$data.selectedPost.title = "changed";

The textbox does not reflect the assigned value. Why is this happening and how can I fix it?

You can see the issue in action in this jsfiddle link: https://jsfiddle.net/raphadko/3fLvfea2/

Answer №1

The reason for the observed behavior is that Vue's reactivity system cannot detect properties added to objects after Vue has been initialized, unless you use $set. To resolve this issue, simply initialize the title property.

var vueapp = new Vue({
  el: '#app',
  data: { 
          selectedPost: {title:''} 
        }
});

Check out the updated version on JSFiddle.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Ways to verify the occurrence of a successful event following the execution of a save() operation on a model

Below is the snippet of code I am using to store extra properties in a model (specifically, the answer model) selectMedia: => # Post media to server @options.answer.id = @options.answer.get('_id') @options.answer.url = "/v1/answers/#{@o ...

I am having an issue with the npm install command. Each time I try running it, I keep receiving an

After posting the output, I find myself unable to comprehend anything. Can someone please guide me on what steps to take next? npm has issued a warning about an old lockfile and advises that supplemental metadata needs to be fetched from the registry due t ...

What is the best approach for testing the TypeScript code below?

Testing the following code has been requested, although I am not familiar with it. import AWS from 'aws-sdk'; import db from './db'; async function uploadUserInfo(userID: number) { const user = db.findByPk(userID); if(!user) throw ...

Having trouble with NPM Moment.js in React: Why is moment__WEBPACK_IMPORTED_MODULE_2__format not functioning properly?

scenario, After resolving my current issue using dateFns (date-fns.org), I am interested in integrating Momentjs into the project as well. The task at hand involves converting data to a string format within an app built with create-react-app. However, wh ...

What is the process for converting a canvas to an image and uploading it to Flask?

Hey there, I have a question regarding uploading a resized canvas image as a file to Flask. Initially, I attempted to use the canvas.toDataURL() method to convert it into a base64 string and then uploaded it as an image using formdata with AJAX, but unfor ...

What are some ways to keep my attention on a validated text box without restricting other text boxes?

Below is a snippet of a validation method that I currently incorporate: if(currentFieldCategory=='e') { var atpos=currentFieldValue.indexOf("@"); var dotpos=currentFieldValue.lastIndexOf("."); if (atpos<1 || dotpo ...

A step-by-step guide on integrating vuetify-component into codeceptjs

When attempting to write tests using codecept.js, I am facing difficulties accessing the vuetify components. <v-layout> <v-flex xs7> <v-text-field ref="video1min" v-model="video1min" :rules=" ...

Element vanishing post JSON parsing

I have encountered an intriguing issue: I have developed an HTML/JS project that extracts information using XMLHttpRequest from the Scratch website. The main objective is to allow users to input their username and view their profile description on Scratc ...

Tips for previewing selected image files before uploading them in Laravel Nova 4

How can I display a preview of the image selected before submitting the form in Laravel Nova 4? # App/Nova/Image.php /** * Get the fields displayed by the resource. * * @param \Laravel\Nova\Http\Requests\NovaRequest $request ...

Why am I receiving an error when trying to obtain context from this particular item?

I need help creating a line chart using Chart.js in Vue.js. The chart should be based on price and date data. Below is the HTML code I currently have: <div class="content"> <section class="content"> < ...

Passing a table value to a PHP script using JQuery on click event

I am struggling with implementing functionality to filter a dataset based on the link that the user clicks on within a bootstrap modal. The modal contains a morris.js graph and I need to pass the clicked value (e.g. Cluster1 or Cluster2) to a data pull scr ...

Guide on transforming UTC time from the server to the local time of users during a GET request

I am currently facing a challenge where I need to verify if the date of the last time an element was clicked matches the current date. Due to my server generating the current date which is 5 hours ahead of my local time, there is a discrepancy causing the ...

When using Vue.js, scrolling inside the modal will seamlessly scroll the content of the modal's background as well

When you click on the image, a long scrolling modal appears. The issue is that when you scroll in the modal, it also scrolls the background of the modal. How can this be fixed? The Modal component is included in the code below: Carousel.vue <templa ...

How can I incorporate arithmetic operators within a function in EJS?

Currently, I am developing an express app that includes a booking form using ejs with added functionality for payment processing. In the code, I have a select tag where the selected text is stored in a variable. Although console logging shows the value co ...

Dynamic SQL queries are creating a new layer of security

Currently, I am working on developing a sidebar search feature wherein users can select specific options by clicking them. These selected variables are then used to generate an SQL query. Here is the process in more detail: 1. The user chooses options ...

The performance of my SVG Filter is dismal

Here is the SVG filter I am using: <svg style="visibility: hidden; height: 0; width: 0;"> <filter id="rgbShift"> <feOffset in="SourceGraphic" dx="1" dy="-1" result="text1" /> <feFlood flood-color="#FF0000" result=" ...

Mastering the art of inserting or updating data in a database using ExpressJS

I am trying to update the database value if it does not exist, and insert data if it does. I have attempted a method but unfortunately, it was not successful. Can anyone help me figure out how to achieve this efficiently? fetchRecordFromFile.forEach(el = ...

The jQuery code functions properly only if it is inserted directly into the console

I'm facing an issue with a Mobile Font markup switch function. It works fine in the console, but not when I run it within a document ready function or call the function separately. It's strange that I have to paste the code directly into the con ...

Do these exports serve the same purpose?

Can someone help me understand why one export works while the other doesn't? I've tried to figure it out on my own but I'm stuck. Functioning Example const dataStore = new Vapi({ baseURL: 'http://domain.test/api', state: ...

JQuery If Statement always outputs a consistent number regardless of the input provided

I'm facing an issue with my HTML form and JQuery code that is supposed to calculate a figure. The problem I am encountering is that the if statement always returns the same number, regardless of the input: $(document).ready(function() { ...