2

i have problem in giving line break in string while binding the data in angularJS

<h3  ng-bind="thirdContainHeaderOneTitle"></h3> 
$scope.thirdContainHeaderOneTitle = "my + '<br>' + hdjsd";

the <br>,'<br>',' \/n' are not working and i dont know why..

3 Answers 3

3

See https://docs.angularjs.org/api/ng/directive/ngBindHtml along with the posted example:

View:

<div ng-controller="ngBindHtmlCtrl">
 <p ng-bind-html="myHTML"></p>
</div>

Application:

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>';
}]);

Note that you need to include the ngSanitize module.

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

Comments

0

You can't just bind html in your strings. Try something like this instead.

<div ng-bind-html-unsafe="thirdContainHeaderOneTitle"></div>

That should parse out your HTMl and give the effect you want.

Comments

0

You cannot sent any html code through the $scope

Its better you use a directive and inside of that , there will be a template, then you can send any html you want like this :

  app.directive('yourdirective',function(){
      return {
        restrict:"E",
        template:"my" + "<br>" + "hdjsd"
        } 
   });

and in your html :

 <yourdirective></yourdirective>

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.