• angularjs的双向绑定原理实现


    angularjs的双向绑定用js代码来实现

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8" />
        <title>双向绑定的js实现</title>
        <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
    
    </head>
    <body>
        <input type="text" id="txt1" />
    
        <input type="text" id="txt2" />
    
    
    
        <script type="text/javascript">
            window.onload = function () {
                let a = '';
                let oTxt = document.getElementById('txt1');
                let oTxt2 = document.getElementById("txt2");
    
    
                function $interval(fn, time) {
                    setInterval(function () {
                        fn();
                        apply();
                    }, time
                    );
                }
    
                function $http(url, success, error) {
                    $.ajax({
                        url,
                        dataType: 'json',
                        success(result) {
                            success(result);
                            apply();
                        },
                        error
                    });
    
                }
    
                //数据->input
    
                function apply() {
                    oTxt.value = a;
                    oTxt2.value = a;
                }
    
                //$interval(function () {
                //    a +='|';
                //}, 100);
    
    
    
                //$.ajax({
                //    url: 'Data/1.txt',
                //    dataType: 'json',
                //    success(res) {
                //        a = res;
                //        apply();
    
                //    },
                //    error() {
                //        alert("错了");
                //    }
                //});
    
    
                //$http('Data/1.txt', function (arr) {
                //    a = arr;
                //}, function () {
                //    alert('错了');
                //});
    
    
    
                //setInterval(function () {
                //    a += '|';
                //    apply();
                //}, 100);
    
    
    
    
                //input->数据
    
                oTxt.oninput = function () {
                    a = this.value;
                    apply();
                }
    
                oTxt2.oninput = function () {
                    a = this.value;
                    apply();
                }
                setInterval(function () {
                    console.log(a);
                }, 100);
            }
        </script>
    
    </body>
    </html>

    升级版的(封装了一下)

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8" />
        <title>angularjs的双向绑定实现</title>
        <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
    </head>
    <body>
    
        <input type="text" ng-model="a" />
        <input type="text" ng-model="a" />
    
        <select ng-model="a">
    
            <option value="1">北京</option>
            <option value="2">上海</option>
            <option value="3">广州</option>
            <option value="4">深圳</option>
    
        </select>
    
        <br />
        <br />
        <br />
        <br />   <br />
        <br />   <br />
        <br />
    
    
        <input type="text" ng-model="b" />
    
    
        <select ng-model="b">
    
            <option value="1">北京</option>
            <option value="2">上海</option>
            <option value="3">广州</option>
            <option value="4">深圳</option>
    
        </select>
    
        <script type="text/javascript">
            window.onload = function () {
                let $scope = {};
                let aEle = document.getElementsByTagName('*');
    
    
                //数据->input
                function apply() {
                    Array.from(aEle).forEach(ele => {
                        let model = ele.getAttribute('ng-model');
                        if (model) {
                            if ($scope[model]) {
                                ele.value = $scope[model];
                            }
                            else {
                                ele.value = '';
                            }
                        }
                    });
                }
    
    
                //input->数据
    
                Array.from(aEle).forEach(ele => {
                    let model = ele.getAttribute('ng-model');
                    if (model) {
                        ele.oninput = function () {
                            $scope[model] = this.value;
                            apply();
                        }
                    }
                });
    
            }
    
        </script>
    </body>
    </html>
    sometimes the hardest part isn't letting go,but rather start over
  • 相关阅读:
    省选模板_简单数学
    省选模板大杂烩
    省选_简单算法
    省选_简单图论
    省选_简单数据结构
    BZOJ4545: DQS的trie 广义后缀自动机 + LCT
    BZOJ 4229: 选择 LCT + 独创方法 + 边双
    luoguP2742 【模板】二维凸包 / [USACO5.1]圈奶牛 二维凸包
    python面向过程编程小程序 -ATM(里面用了终端打印)
    从7点到9点写的小程序(用了模块导入,python终端颜色显示,用了点局部和全局可变和不可变作用域,模块全是自定义)
  • 原文地址:https://www.cnblogs.com/zhumeiming/p/9820671.html
Copyright © 2020-2023  润新知