雙向資料繫結實現原理

森林蘑菇_mushroom發表於2019-03-01

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 輸入,模擬資料驅動
雙向資料繫結實現原理

相關文章