在angularjs中使用$ compile

RJV*_*RJV 1 angularjs angularjs-compile

我真的很困惑与angularjs中的$ compile.任何人都可以帮助我,在本文档中使用其他示例的angularjs中的$ compile有什么用处. https://docs.angularjs.org/api/ng/service/$compile

小智 8

$ compile只需将文本编译为html ..

这是示例

 angular
                .module("myModule", [])
                .controller("myController", ['$scope', '$compile', function ($scope, $compile) {
                    $scope.txt = "<b>SampleTxt</b>";
                    $scope.submit = function () {
                        var html = $compile($scope.txt)($scope);
                        angular.element(document.getElementById("display")).append(html);
                    }
                }]);
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="myModule" >
    <div ng-controller="myController">
        <textarea ng-model="txt" ></textarea>
        <input type="button" value="submit" ng-click="submit()" />
        <div id="display"></div>
    </div>
</body>
Run Code Online (Sandbox Code Playgroud)