With ng-bind-html-unsafe removed, how do I inject HTML?

后端 未结 10 2126
不思量自难忘°
不思量自难忘° 2020-11-22 04:06

I\'m trying to use $sanitize provider and the ng-bind-htm-unsafe directive to allow my controller to inject HTML into a DIV.

However, I can

10条回答
  •  没有蜡笔的小新
    2020-11-22 04:30

    The best solution to this in my opinion is this:

    1. Create a custom filter which can be in a common.module.js file for example - used through out your app:

      var app = angular.module('common.module', []);
      
      // html filter (render text as html)
      app.filter('html', ['$sce', function ($sce) { 
          return function (text) {
              return $sce.trustAsHtml(text);
          };    
      }])
      
    2. Usage:

      
      

    Now - I don't see why the directive ng-bind-html does not trustAsHtml as part of its function - seems a bit daft to me that it doesn't

    Anyway - that's the way I do it - 67% of the time, it works ever time.

提交回复
热议问题