最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - AngularJs Return HTML from Controller - Stack Overflow

programmeradmin4浏览0评论

I try to return some HTML code from my AngularJs controller to the html data.

This is path of my html :

<div align="right">
   {{chooseHtmlElement()}}">
</div>

And this is path of my AngularJs Controller:

   $scope.chooseHtmlElement= function () {
        var sum = $scope.chiQuadSum();
        if (isNaN(sum)) {
            return "";
        }
        if (sum > 17.00) {
            return "<i class=\"fa fa-minus-circle\" style=\"color:red;\"></i>";
        } else if (sum < 14.00) {
            return "<i class=\"fa fa-check-circle\" style=\"color:green;\"></i>";
        } else {
            return "<i class=\"fa fa-circle\" style=\"color:orange;\"></i>";
        }
    }

The problem is when I return these string, the element is not shown as html element, but as text which you can read. Is there any possibility returning these string as html element?

I try to return some HTML code from my AngularJs controller to the html data.

This is path of my html :

<div align="right">
   {{chooseHtmlElement()}}">
</div>

And this is path of my AngularJs Controller:

   $scope.chooseHtmlElement= function () {
        var sum = $scope.chiQuadSum();
        if (isNaN(sum)) {
            return "";
        }
        if (sum > 17.00) {
            return "<i class=\"fa fa-minus-circle\" style=\"color:red;\"></i>";
        } else if (sum < 14.00) {
            return "<i class=\"fa fa-check-circle\" style=\"color:green;\"></i>";
        } else {
            return "<i class=\"fa fa-circle\" style=\"color:orange;\"></i>";
        }
    }

The problem is when I return these string, the element is not shown as html element, but as text which you can read. Is there any possibility returning these string as html element?

Share Improve this question edited Oct 26, 2015 at 17:53 Pankaj Parkar 136k23 gold badges240 silver badges303 bronze badges asked Oct 22, 2015 at 9:28 ANEDevANEDev 1111 silver badge6 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 11

While binding html on view in angular js you need to use ng-bind-html directive. But before binding html, you need to sanitize that html by using $sce.trustAsHtml method of ngSanitize module.

<div align="right" ng-bind-html="chooseHtmlElement() | trustedhtml"></div>

Filter

app.filter('trustedhtml', 
   function($sce) { 
      return $sce.trustAsHtml; 
   }
);

you can use

ngSanitize

There are 2 steps:

  1. include the angular-sanitize.min.js resource, i.e.:
    <script src="lib/angular/angular-sanitize.min.js"></script>

  2. In a js file (controller or usually app.js), include ngSanitize, i.e.:
    angular.module('myApp', ['myApp.filters', 'myApp.services', 'myApp.directives', 'ngSanitize'])

$refrence

Instead of returning element from controller to HTML use $element to add/append content from controller to HTML. Find your DIV element using angular.element.

var tag = your return elements.

var ele = $pile(tag)($scope);
angular.element('div').append(ele);

For Sanitizing an html string , Include following module and js in your code,

<script src="//code.angularjs/1.4.7/angular-sanitize.js"></script>

angular.module('app', [
  'ngSanitize'
]); 

Bind scope like,

<div align="right" ng-bind-html="chooseHtmlElement()"></div>
发布评论

评论列表(0)

  1. 暂无评论