原生JS实现angular的双向数据绑定

window. onload = function (){
var allEle = document. getElementsByTagName ( "*" );
var $scope = {};

function $apply (){
Array . from (allEle). forEach ( ele => {
var model = ele. getAttribute ( 'ng-model' );
if (model){
if ($scope[model]){
ele.value = $scope[model];
} else {
ele.value = "" ;
}
}
});
}

Array . from (allEle). forEach ( ele => {
var model = ele. getAttribute ( "ng-model" );
if (model){
ele. oninput = function (){
$scope[model] = this.value;
$apply ();
}
}
})

}

你可能感兴趣的:(原生JS实现angular的双向数据绑定)