Simple implementation of jquery and js is similar to Angular. js bidirectional binding

  • 2021-07-10 18:51:34
  • OfStack

Just learned about Angular. js, and found that Angular. js is very convenient in binding data. Set up the tutorial demo:


<div ng-app="myApp" ng-controller="myCtrl">
   Name : <input ng-model="name">
 <h1> You typed in : {{name}}</h1>
</div>

I just thought about how to achieve a similar effect with jq/js, and then looked for it and found that it could be achieved with propertychange.

JQ:


<div class="wrap">
    <textarea></textarea>
    <div class="miss"></div>
 </div>

 $('textarea').on('input propertychange', function() {
   $('.miss').html($(this).val().length + "~"+$(this).val());
 });

JS:


var txt = document.querySelector("textarea"),
    msg = document.querySelector(".miss");
  // Incompatible IE8  The following 
  txt.addEventListener("input",function () {
    msg.innerHTML = this.value + "~"+this.value.length;
  },false)

Related articles: