Working with $compile in angularjs

社会主义新天地 提交于 2019-12-03 09:10:51

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!