【问题标题】:Working with $compile in angularjs在 angularjs 中使用 $compile
【发布时间】:2016-10-18 05:16:40
【问题描述】:

我真的对 angularjs 中的 $compile 感到困惑。 任何人都可以帮助我,在 angularjs 中 $compile 有什么用,举个例子 其他然后在本文档中。 https://docs.angularjs.org/api/ng/service/$compile

【问题讨论】:

    标签: angularjs angularjs-compile


    【解决方案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>

    【讨论】:

    • 如果我在文本框中有文本,如 {{SampleTxt}}。我的意思是花括号。 $compile 后如何在 UI 中查看大括号的值。
    • 是否可以从 AngularJS 外部的 JavaScript 调用 $compile 服务作为正常功能?我可以使用angular.element() 访问范围,但是如何访问$compile 服务来创建 HTML 元素、编译并插入它或在修改后替换现有的?
    【解决方案2】:

    这个链接的第一行https://docs.angularjs.org/api/ng/service/$compile已经说了关于$compile的全部内容

    简而言之,它将编译 html dom,以便它将用于 Angular js 的范围。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-15
    • 2015-09-07
    • 2014-04-17
    • 1970-01-01
    • 2019-02-19
    相关资源
    最近更新 更多