javascript - 从范围内的变量渲染 Angular 的 html

标签 javascript angularjs

我知道与我的请求相关的安全隐患,但我需要允许特殊的 super 管理用户组能够创建和评估当前 $scope 变量中包含的 Angular html

这是一个示例 plunk :

<!DOCTYPE html>
<html ng-app="plunker">

  <head>
    <meta charset="utf-8" />
    <title>AngularJS Plunker</title>
    <script>document.write('<base href="' + document.location + '" />');</script>
    <link rel="stylesheet" href="style.css" />
    <script data-require="angular.js@1.3.x" src="https://code.angularjs.org/1.3.15/angular.js" data-semver="1.3.15"></script>
    <script src="app.js"></script>
  </head>

  <body ng-controller="MainCtrl" ng-init="variable = 3; content = '{{ variable }}'">
    <div>
      The value of $scope.variable === "{{ variable }}"
    </div>
    <div>
      The value of $scope.content === "{{ content }}"
    </div>
    <br>
    <div>
    The value of $scope.content is <b>ng-model</b>'ed via the following textarea:<br>
    </div>

    <textarea rows="3" ng-model="content"></textarea>

    <div style="border: 1px solid black">
      Instead of rendering the value of the $scope.content field which is currently equal to "{{ content }}" I need to render compiled and evaluated value which should be equal to "{{ variable }}"
    </div>
  </body>

</html>

有什么建议吗?

提前谢谢您!

最佳答案

您可以创建一个使用 $compile 服务的指令来为您执行此操作。

这是我想出的,和 a fork of your Plunkr以证明它有效。

app.directive('compile', function($compile) {
  return {
    restrict: 'A',
    link: function(scope, elem, attrs) {
      var prevScope;
      scope.$watch(attrs.compile, function(newVal, oldVal) {
        // create a span (an inline element) so we have an actual DOM node to
        // set the innerHTML of.
        var newElem = document.createElement('span');
        newElem.innerHTML = newVal;
        // clean up first
        if (prevScope) {
          prevScope.$destroy();
          prevScope = null;
        }
        // clear out the contents of this element
        elem.empty();
        // and replace it with the raw (uncompiled) node
        elem[0].appendChild(newElem);
        // now the node is in the DOM so we can compile it
        // but we want to use a try..catch because the user
        // might be in the middle of typing a new expression,
        // but the syntax right now is not valid so the
        // expression parser will throw an error.
        try {
          // compile the node in the DOM with a child of the existing scope
          prevScope = scope.$new();
          $compile(newElem)(prevScope);
        } catch (e) { /* don't need to do anything here */ }
      });
    }
  }
});

关于javascript - 从范围内的变量渲染 Angular 的 html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29189815/

相关文章:

javascript - 使用 jQuery 在加载时设置 CSS 属性

javascript - 使用 Angular 和 JSON 避免再次调用 API

javascript - AngularJS:手动输入时如何验证日期格式(DD/MM/YYYY)

javascript - setTimeout() 在 angularjs 中不起作用?

javascript - 何时在 Angularjs 中使用 $q.defer()

javascript - 在Fabric.js中输出canvas JSON中的特定变量

javascript - 使用 javascript 从 Firebase 添加地点到热图

javascript - 使用 angularJS 不会删除 mongodb 中的数据

javascript - jQuery Mobile 禁用某些标签的增强功能?

c# - 从 Javascript 方法调用 c# 函数