1

How do I push HTML within the Angular variable?

controler.js

function myControler($scope){

   $scope.myItem = '<p>' + Test + '</p>';
}

view.html

<div ng-controller="myControler">{{myItem}}</div>
0

2 Answers 2

2

Generally, you shouldn't.

It's the view's responsibility to generate HTML. The controller should expose structured data to the view, through the scope. It shouldn't generate HTML.

If that's really not an option, then use the ng-bind-html directive and the $sce service. The example code shows exactly what you want to do.

Sign up to request clarification or add additional context in comments.

Comments

0

Try using the directive ngBindHtml

angular.module('ngBindHtmlExample', ['ngSanitize'])

.controller('ngBindHtmlCtrl', ['$scope', function ngBindHtmlCtrl($scope) {
  $scope.myHTML =
     'I am an <code>HTML</code>string with <a href="#">links!</a> and other <em>stuff</em>';
}]);

More infornation on ngBindHtml: http://docs.angularjs.org/api/ng/directive/ngBindHtml

Or you can create a custom directive: http://docs.angularjs.org/guide/directive http://plnkr.co/edit/ngdoc:example-example83@snapshot?p=preview

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.