小编典典

在angularjs中使用$ compile

angularjs

我真的对angularjs中的 $ compile 感到困惑。任何人都可以帮助我,在angularjs中使用$
compile以及在本文档之外的示例。
https://docs.angularjs.org/api/ng/service/$compile


阅读 341

收藏
2020-07-04

共1个答案

小编典典

$ 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);

                    }

                }]);


<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>
2020-07-04