【问题标题】:Insert and parse HTML into view using AngularJS使用 AngularJS 将 HTML 插入并解析到视图中
【发布时间】:2013-09-12 12:05:08
【问题描述】:

我知道当我想在视图中插入 HTML 时,我使用 'ng-bind-html''ng-bind-html-unsafe'

我不知道的是如何插入 HTML 并让 Angular 解析其内容

即如果有'ng-repeat',我想让Angular解析它?

更新 1:

示例:

HTML:

<div ng-repeat="t in ts" ng-bind-html-unsafe="t.html()"></div>

JS:

function Controller($scope) {
    $scope.ts = {obj1: new obj(), obj2: new obj(), ob3: new obj()};

}

function obj() {
    // which may be "<div ng-repeat="s in somthing">{{s}}</div>"
    // or "<ul><li ng-repeat="s in somthing">{{s.k}}</li></ul>"
    // or something else
    var _html;

    this.html = function() {
        return _html;
    }
}

我尝试使用上述方法,但 Angular 只是按原样打印 {{s}}{{s.k}}

【问题讨论】:

    标签: angularjs html-parsing


    【解决方案1】:

    您可以使用$compile 服务 (docs) 将任意 HTML 编译成角度视图。

    app.run(function($rootScope, $compile, $rootElement) {
      // We'll create a new scope to use as the context for the view.
      $scope = $rootScope.$new();
      $scope.model = [{name: 'first'}, {name: 'second'}, {name: 'third'}];
    
      // Calling `$compile(html)` returns a function that, when called with
      // a context object, links the compiled HTML to the given context (e.g.
      // binds scope-based expressions in the view to the passed in scope).
      var html = "<div ng-repeat='m in model'>{{m.name}}</div>";
      var linkingFunction = $compile(html);
      var elem = linkingFunction($scope);
    
      // You can then use the DOM element like normal.
      $rootElement.append(elem);
    });
    

    在这种情况下,我已将视图附加到 $rootElement(这是引导模块时使用的元素,通常由 ng-app 指令);在许多情况下,您将在指令的链接函数中执行此类操作,并且可以访问相关元素。当然,您可以使用 jQuery 或 jqLit​​e 获取原始 HTML,但请记住在链接范围内至少允许一个摘要循环(否则 HTML 尚未使用范围中的值更新) .

    工作示例:http://jsfiddle.net/BinaryMuse/QHhVR/

    ng-include 指令的内部,Angular's doing this very thing

    $compile(currentElement.contents())(currentScope);
    

    [更新]

    这是一个更完整的示例,展示了更接近您更新的问题的内容:

    app.controller("MainController", function($scope) {
      $scope.ts = [
        {
          elements: ['one', 'two', 'three'],
          html: '<div ng-repeat="elem in t.elements">{{elem}}</div>'
        },
        {
          things: [8, 9, 10],
          add: function(target) {
            var last = target[target.length - 1];
            target.push(last + 1);
          },
          html: '<ul><li ng-repeat="num in t.things">{{num}}</li>' +
            '<li><button ng-click="t.add(t.things)">More</button></li></ul>'
        }
      ];
    });
    
    app.directive("bindCompiledHtml", function($compile, $timeout) {
      return {
        template: '<div></div>',
        scope: {
          rawHtml: '=bindCompiledHtml'
        },
        link: function(scope, elem, attrs) {
          scope.$watch('rawHtml', function(value) {
            if (!value) return;
            // we want to use the scope OUTSIDE of this directive
            // (which itself is an isolate scope).
            var newElem = $compile(value)(scope.$parent);
            elem.contents().remove();
            elem.append(newElem);
          });
        }
      };
    });
    
    <div ng-controller="MainController">
      <div ng-repeat="t in ts" bind-compiled-html="t.html"></div>
    </div>
    

    工作示例:http://jsfiddle.net/BinaryMuse/VUYCG/

    HTML sn-ps 使用t.elementst.things 毫无价值,因为t 是由外部 HTML 中的ng-repeat 创建的范围值。如果你愿意,你可以做一些范围体操,让它变得更好。

    【讨论】:

    • 更新了 bindCompiledHtml 指令以支持最小化:jsfiddle.net/zm2e1uoo
    • 这个解决方案就像一个魅力。当我尝试解析包含角度指令的html时。然后就解析成功了。
    【解决方案2】:

    如果重复元素的选项数量有限,则可以使用 ng-switch 代替,如果 HTML 来自服务器,则可以使用 ng-include。

    【讨论】:

    【解决方案3】:

    这样的?

    <div ng-repeat="item in items">
        <div ng-bind-html-unsafe="item.html"></div>
    </div>
    

    在哪里

    items = [
        {
            html: 'test<br/>test'
        },
        {
            html: 'test2<br/>test2'
        }
    ]
    

    【讨论】:

    • 关闭,html 将包含类似 '
      {{m.name}}
      '
    猜你喜欢
    • 2012-03-12
    • 2017-03-24
    • 1970-01-01
    • 2013-05-31
    • 1970-01-01
    • 1970-01-01
    • 2013-02-10
    相关资源
    最近更新 更多