2 way data binding in JavaScript

后端 未结 5 2202
栀梦
栀梦 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 10:07

    Simple and working approach to two-way binding, only using vanilla JS.

    
    

    // vm.js - vanialla JS
    let vm = {
        _username: "",
        get username() {
            return this._username;
        },
        set username(value) {
            this._username = value;
        },
        onSubmit: function (event, element) {
            console.log(this.username);
        }
    }
    

    JS Getters and Setters are quite nice for this - especially when you look at the browser support.

提交回复
热议问题