2 way data binding in JavaScript

后端 未结 5 2178
栀梦
栀梦 2020-12-18 09:11

Two-way data binding refers to the ability to bind changes to an object’s properties to changes in the UI, and vice-versa.

Can we achieve 2-way data-binding with Jav

5条回答
  •  时光说笑
    2020-12-18 09:47

    When an input is changed update the value, add a setter to the value which sets the inputs content. E.g this element:

    
    

    And some js:

    var person = (function(el){
     return {
       set age(v){
        el.value = v;
       },
       get age(){
         return el.value;
       }
     };
    })(document.getElementById("age"));
    

    So you can do:

     person.age = 15;
    

    And the input will change. Changing the input changes person.age

提交回复
热议问题