How to create angularjs filter which outputs HTML

后端 未结 3 1112
庸人自扰
庸人自扰 2020-12-13 01:14

After reading AngularJS tutorial step-9 I have created my own AngularJS filter, which should convert boolean data into html.

Here is my filter code:

         


        
相关标签:
3条回答
  • 2020-12-13 01:55

    You should use the ng-bind-html directive (require to import the sanitize module and js file): https://docs.angularjs.org/api/ng/directive/ngBindHtml

    <span ng-bind-html='phone.connectivity.infrared | iconify'></span>
    

    You also need to import the CSS (Bootstrap I guess) to be able to see the icon when it works.

    I have provided a working example.

    0 讨论(0)
  • 2020-12-13 01:57

    Try this filter

    filter('trust', ['$sce',function($sce) {
      return function(value, type) {
        return $sce.trustAs(type || 'html', value);
      }
    }]);
    

    requires angular-sanitize

    var app = angular.module("myApp", ['ngSanitize']);
    

    Gist Link

    0 讨论(0)
  • 2020-12-13 02:00

    unless I am reading it wrong, you are approaching in the wrong way

    I think ng-class is directive you need for this job and is safer then rendering to class attribute.

    in your case just add object string with the id strings as the class and the value as the evaluated expression

    <i ng-class="{
    'icon-ok':!phone.connectivity.infrared,
    'icon-remove':phone.connectivity.infrared
    }"></i>'
    

    on a side note, you should only use directives (built-in and custom) to manipulate html/dom and if you needed a more complex html render you should look at directive instead

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