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

前端之家收集整理的这篇文章主要介绍了使用AngularJS插入并解析HTML到视图前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我所知道的是当我想将HTML插入到视图中时,我使用’ng-bind-html’或’ng-bind-html-unsafe’。

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

即如果有’ng-repeat’,我想要Angular来解析呢?

更新1:

例:

HTML:

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

JS:

  1. function Controller($scope) {
  2. $scope.ts = {obj1: new obj(),obj2: new obj(),ob3: new obj()};
  3.  
  4. }
  5.  
  6. function obj() {
  7. // which may be "<div ng-repeat="s in somthing">{{s}}</div>"
  8. // or "<ul><li ng-repeat="s in somthing">{{s.k}}</li></ul>"
  9. // or something else
  10. var _html;
  11.  
  12. this.html = function() {
  13. return _html;
  14. }
  15. }

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

您可以使用$ compile服务( docs)将任意HTML编译成角度视图。
  1. app.run(function($rootScope,$compile,$rootElement) {
  2. // We'll create a new scope to use as the context for the view.
  3. $scope = $rootScope.$new();
  4. $scope.model = [{name: 'first'},{name: 'second'},{name: 'third'}];
  5.  
  6. // Calling `$compile(html)` returns a function that,when called with
  7. // a context object,links the compiled HTML to the given context (e.g.
  8. // binds scope-based expressions in the view to the passed in scope).
  9. var html = "<div ng-repeat='m in model'>{{m.name}}</div>";
  10. var linkingFunction = $compile(html);
  11. var elem = linkingFunction($scope);
  12.  
  13. // You can then use the DOM element like normal.
  14. $rootElement.append(elem);
  15. });

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

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

在ng-include指令的下方,Angular’s doing this very thing

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

[更新]

这是一个更完整的例子,它演示了一些更接近你更新的问题:

  1. app.controller("MainController",function($scope) {
  2. $scope.ts = [
  3. {
  4. elements: ['one','two','three'],html: '<div ng-repeat="elem in t.elements">{{elem}}</div>'
  5. },{
  6. things: [8,9,10],add: function(target) {
  7. var last = target[target.length - 1];
  8. target.push(last + 1);
  9. },html: '<ul><li ng-repeat="num in t.things">{{num}}</li>' +
  10. '<li><button ng-click="t.add(t.things)">More</button></li></ul>'
  11. }
  12. ];
  13. });
  14.  
  15. app.directive("bindCompiledHtml",function($compile,$timeout) {
  16. return {
  17. template: '<div></div>',scope: {
  18. rawHtml: '=bindCompiledHtml'
  19. },link: function(scope,elem,attrs) {
  20. scope.$watch('rawHtml',function(value) {
  21. if (!value) return;
  22. // we want to use the scope OUTSIDE of this directive
  23. // (which itself is an isolate scope).
  24. var newElem = $compile(value)(scope.$parent);
  25. elem.contents().remove();
  26. elem.append(newElem);
  27. });
  28. }
  29. };
  30. });
  1. <div ng-controller="MainController">
  2. <div ng-repeat="t in ts" bind-compiled-html="t.html"></div>
  3. </div>

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

HTML片段使用t.elements和t.things是不值得的,因为t是由外部HTML中的ng-repeat创建的范围值。你可以做一些范围体操,使它更好一点,如果你喜欢。

猜你在找的Angularjs相关文章