一尘不染

使用AngularJS将HTML插入并解析到视图中

angularjs

我所知道的是,当我想在视图中插入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}}按原样打印。


阅读 144

收藏
2020-07-04

共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正在做这件事

$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片段使用t.elements,并t.things因为t是由创建范围值ng-repeat外部
HTML。如果愿意,您可以进行一些体操锻炼,以使其变得更好一点。

2020-07-04