angular.js ng-repeat li items with html content

前端 未结 7 1721
挽巷
挽巷 2020-12-13 18:34

I have a model that comes back from the server which contains html instead of text (for instance a b tag or an i tag)
when I use ng-repeat to built a list out of it i

相关标签:
7条回答
  • 2020-12-13 18:55

    It goes like ng-bind-html-unsafe="opt.text":

    <div ng-app ng-controller="MyCtrl">
        <ul>
        <li ng-repeat=" opt in opts" ng-bind-html-unsafe="opt.text" >
            {{ opt.text }}
        </li>
        </ul>
    
        <p>{{opt}}</p>
    </div>
    

    http://jsfiddle.net/gFFBa/3/

    Or you can define a function in scope:

     $scope.getContent = function(obj){
         return obj.value + " " + obj.text;
     }
    

    And use it this way:

    <li ng-repeat=" opt in opts" ng-bind-html-unsafe="getContent(opt)" >
         {{ opt.value }}
    </li>
    

    http://jsfiddle.net/gFFBa/4/

    Note that you can not do it with an option tag: Can I use HTML tags in the options for select elements?

    0 讨论(0)
  • 2020-12-13 18:56

    Note that ng-bind-html-unsafe is no longer suppported in rc 1.2. Use ng-bind-html instead. See: With ng-bind-html-unsafe removed, how do I inject HTML?

    0 讨论(0)
  • 2020-12-13 19:05

    If you want some element to contain a value that is HTML, take a look at ngBindHtmlUnsafe.

    If you want to style options in a native select, no it is not possible.

    0 讨论(0)
  • 2020-12-13 19:09

    ng-bind-html-unsafe is deprecated from 1.2. The correct answer should be currently:

    HTML-side: (the same as the accepted answer stated):

    <div ng-app ng-controller="MyCtrl">
       <ul>
          <li ng-repeat=" opt in opts" ng-bind-html-unsafe="opt.text">
            {{ opt.text }}
          </li>
       </ul>
    
       <p>{{opt}}</p>
    </div>
    

    But in the controller-side:

    myApp.controller('myCtrl', ['$scope', '$sce', function($scope, $sce) {
    // ...
       $scope.opts.map(function(opt) { 
          opt = $sce.trustAsHtml(opt);
       });
    }
    
    0 讨论(0)
  • 2020-12-13 19:14

    Here is directive from the official examples angular docs v1.5 that shows how to compile html:

    .directive('compileHtml', function ($compile) {
      return function (scope, element, attrs) {
        scope.$watch(
          function(scope) {
            return scope.$eval(attrs.compileHtml);
          },
          function(value) {
            element.html(value);
            $compile(element.contents())(scope);
          }
        );
      };
    });
    

    Usage:

    <div compile-html="item.htmlString"></div>
    

    It will insert item.htmlString property as html any place, like

    <li ng-repeat="item in itemList">
        <div compile-html="item.htmlString"></div>
    
    0 讨论(0)
  • 2020-12-13 19:19

    You can use NGBindHTML or NGbindHtmlUnsafe this will not escape the html content of your model.

    http://jsfiddle.net/n9rQr/

    <div ng-app ng-controller="MyCtrl">
        <ul>
        <li ng-repeat=" opt in opts"  ng-bind-html-unsafe="opt.text">
            {{ opt.text }}
        </li>
        </ul>
    
        <p>{{opt}}</p>
    </div>
    

    This works, anyway you should be very careful when using unsanitized html content, you should really trust the source of the content.

    0 讨论(0)
提交回复
热议问题