Working with $compile in angularjs

前端 未结 2 572
小鲜肉
小鲜肉 2021-01-01 07:19

I am really confused with $compile in angularjs. can anyone help me, What use of $compile in angularjs with an example other then in this documentation. ht

相关标签:
2条回答
  • 2021-01-01 07:51

    The first line of this link https://docs.angularjs.org/api/ng/service/$compile already said all about $compile

    in simple word it will compile html dom so that it will use for scope of angular js.

    0 讨论(0)
  • 2021-01-01 07:56

    $compile just compile the text to html..

    Here is sample example

     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>

    0 讨论(0)
提交回复
热议问题