angularjs动态添加节点时,绑定到$scope中

<html>
    <head>
        <meta charset="utf-8"/>
        <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
        <script src="https://cdn.bootcss.com/angular.js/1.6.8/angular.min.js"></script>

        <script>
            angular.module("testApp", []).controller("testController", function($scope, $compile) {
                $scope.a = "111";
                $scope.b = "222";

                var node = "<input id='input2' ng-model='b' />";
                $("#input1").after(node);

                var linkFn = $compile(node);
                var content = linkFn($scope);
                $("#input2").after(content);
            });
        </script>
    </head>
    <body ng-app="testApp">
        <div ng-controller="testController">
            <input id="input1" ng-model="a" />
        </div>
    </body>
</html>

运行结果:

原文地址:https://www.cnblogs.com/white-knight/p/8341233.html