JavaScript中的2路数据绑定 [英] 2 way data binding in JavaScript

查看:59
本文介绍了JavaScript中的2路数据绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

双向数据绑定是指将对象属性的更改绑定到UI中的更改的能力,反之亦然。

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

我们能实现2-使用JavaScript进行数据绑定的方式?

Can we achieve 2-way data-binding with JavaScript?

特别是没有框架的双向数据绑定。

Especially 2 Way Data Binding without Frameworks.

推荐答案

更改输入时更新值,将setter添加到设置输入内容的值。例如:

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

<input id="age">

还有一些js:

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

所以你可以这样做:

 person.age = 15;

输入将会改变。更改输入更改person.age

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

这篇关于JavaScript中的2路数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆