DEV Community

HarmonyOS
HarmonyOS

Posted on

[Smart Watch] [API 6] The input component of the JS is bound to a variable. After content is entered, the obtained...

Read the original article:[Smart Watch] [API 6] The input component of the JS is bound to a variable. After content is entered, the obtained variable value does not change.

Problem:

An input component is defined in the HTML file. The value attribute of the component is bound to a value. However, when the input content changes, the value obtained from the JS does not change.

Cause:

The data in the HarmonyOS project is unidirectionally bound. If the data is modified in the JS, the data is modified in the HTML file. If the data is modified in the reverse direction, the data cannot be synchronized.

Solution:

Define the change event in the input component. When the input content changes, the change event is triggered. In this case, obtain the current input value from the callback function of the change event and assign the value to the variable.

hml:
<input class="flex" id="input3" type="date" value="{{ inputVal }}" @change="changeInput" />Copy codeCopy code
Enter fullscreen mode Exit fullscreen mode
JS: 
data: {
        inputVal:'InputValText'
    },
    changeInput(obj){
       this.inputVal = obj.value
    }
Enter fullscreen mode Exit fullscreen mode

Written by Erman Derici

Top comments (0)