雙向數據綁定實現原理

small demo

<!DOCTYPE html>
<html>
<head>
	<title>textBind</title>	
</head>
<body>
<input type="text" name="infoInsert"> <input type="button" name="newText" value="FromAjax">
<p id="infoShow"></p>
<script type="text/javascript">
	var obj = {
		seeYou: 'Hello'
	};
	Object.defineProperty(obj, 'infoBind', {
		get: function () {
			return this.seeYou;
		},
		set: function (newValue) {
			document.getElementById('infoShow').innerText = newValue;
			document.getElementsByName('infoInsert')[0].value = newValue;
		}
	});
	document.getElementsByName('infoInsert')[0].addEventListener('keyup', function () {
		obj.infoBind = this.value;
	});
	document.getElementsByName('newText')[0].addEventListener('click', function () {
		obj.infoBind = 'Hello Panda';
	});
</script>
</body>
</html>
複製代碼
  • input text 輸入綁定p標籤

  • Console輸入綁定p標籤

  • input button 輸入,模擬數據驅動

相關文章
相關標籤/搜索