AngularJS displays as text not as a new line

I am sure this has been asked before, but I cannot find the answer.

I have an AngularJS script that pulls from a database and then outputs to the content. Everything works correctly, except for a few places that I am trying to combine several words with new lines between them.

**in the script.js** groupedList[aIndex].CATEGORY = existingCategory+'\n'+thisCategory; groupedList[aIndex].CATEGORY = existingCategory+'<br>'+thisCategory; 

If I use the first line of the above code, I can’t see anything, but there is no new line in the redered html. If I use the second line, <br> obtained as text, and the result is as follows:

 Instinct<br>Media 

instead

 Instinct Media 

I can publish the full script if it were useful, but I assume that there is something simple that I just don't see.

UPDATE Here is the full js

 function qCtrl($scope, $filter, $http, $timeout){ $scope.getCategories = function(){$http.post('quote.cfc?method=getCategories').success(function(data) { $scope.categories = data; }); } $scope.getClassifications = function(){$http.post('quote.cfc?method=getClassifications').success(function(data) { $scope.classifications = data; }); } $scope.getIndustries = function(){$http.post('quote.cfc?method=getIndustries').success(function(data) { $scope.industries = data; }); } $scope.getKeywords = function(){$http.post('quote.cfc?method=getKeywords').success(function(data) { $scope.keywords = data; }); } $scope.getSources = function(){$http.post('quote.cfc?method=getSources').success(function(data) { $scope.sources = data; }); } $scope.getAllQuotes = function(){$http.post('quote.cfc?method=getAllQuotesJoined').success(function(data) { $scope.allQuotes = data; }); } $scope.initScopes = function (){ $scope.getCategories(); $scope.getClassifications(); $scope.getIndustries(); $scope.getKeywords(); $scope.getSources(); $scope.getAllQuotes(); } $scope.initScopes(); // SEARCH QUOTES $scope.filteredQuotes = $scope.allQuotes; $scope.search = {searchField:''}; $scope.searchQuote = function(){ var filter = $filter('filter'); var searchCrit = $scope.search; var newlist = $scope.allQuotes; var groupedList = []; var idList = []; newlist = filter(newlist,{TESTQUOTE:searchCrit.searchField}); for(i=0;i<10;i++){ aIndex = idList.contains(newlist[i].TESTIMONIALID); if(aIndex >= 0){ thisKeyword = newlist[i].KEYWORD; thisClassification = newlist[i].CLASSIFICATION; thisCategory = newlist[i].CATEGORY; existingKeyword = groupedList[aIndex].KEYWORD; existingClassification = groupedList[aIndex].CLASSIFICATION; existingCategory = groupedList[aIndex].CATEGORY; if(thisKeyword != '' && existingKeyword.indexOf(thisKeyword) == -1){ groupedList[aIndex].KEYWORD = existingKeyword+' - '+thisKeyword; } if(thisClassification != '' && existingClassification.indexOf(thisClassification) == -1){ groupedList[aIndex].CLASSIFICATION = existingClassification+' \n '+thisClassification; } if(thisCategory != '' && existingCategory.indexOf(thisCategory) == -1){ groupedList[aIndex].CATEGORY = existingCategory+'<br>'+thisCategory; } } else { idList.push(newlist[i].TESTIMONIALID); groupedList.push(newlist[i]); } } $scope.filteredQuotes = groupedList; } } Array.prototype.contains = function ( needle ) { for (j in this) { if (this[j] == needle) return j; } return -1; } 

Here is the HTML

 <div ng-repeat="q in filteredQuotes" class="well clearfix"> <h3>{{q.TITLE}}</h3> <div class="row-fluid" style="margin-bottom:5px;"> <div class="span3 well-small whBG"><h4>Classification</h4>{{q.CLASSIFICATION}}</div> <div class="span3 well-small whBG pipeHolder"><h4>Categories</h4>{{q.CATEGORY}}</div> <div class="span3 well-small whBG"><h4>Key Words</h4>{{q.KEYWORD}}</div> <div class="span3 well-small whBG"><h4>Additional</h4>Industry = {{q.INDUSTRY}}<br>Source = {{q.SOURCE}}</div> </div> <div class="well whBG">{{q.TESTQUOTE}}</div> <div class="tiny"> Source comment : {{q.SOURCECOMMENT}}<br> Additional Comment : {{q.COMMENT}} </div> </div> </div> 
+42
javascript angularjs
Feb 19 '13 at 17:14
source share
5 answers

I could be wrong because I never used Angular, but I suppose you are probably using ng-bind , which will only create TextNode.

Instead, you want to use ng-bind-html .

http://docs.angularjs.org/api/ngSanitize.directive:ngBindHtml

Refresh . It looks like you need to use ng-bind-html-unsafe='q.category'

http://docs.angularjs.org/api/ng.directive:ngBindHtmlUnsafe

Here is a demo:

http://jsfiddle.net/VFVMv/

+38
Feb 19 '13 at 17:23
source share

You can use \n to concatenate words, and then apply this style to the div container.

 style="white-space: pre;" 

More information can be found at https://developer.mozilla.org/en-US/docs/Web/CSS/white-space

 <p style="white-space: pre;"> This is normal text. </p> <p style="white-space: pre;"> This text contains new lines. </p> 
+44
Apr 17 '15 at 21:21
source share

You need to either use ng-bind-html-unsafe ... or you need to enable the ngSanitize module and use ng-bind-html :

with ng-bind-html-unsafe

Use this if you trust an HTML rendering source that will display the original output of what you put in it.

 <div><h4>Categories</h4><span ng-bind-html-unsafe="q.CATEGORY"></span></div> 



OR with ng-bind-html

Use this if you DO NOT trust the HTML source (i.e. enter it as a user). It will sanitize the html to make sure it does not include things like script tags or other sources of potential security risks.

Make sure you include this:

 <script src="http://code.angularjs.org/1.0.4/angular-sanitize.min.js"></script> 

Then specify it in your application module:

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

THEN use it:

 <div><h4>Categories</h4><span ng-bind-html="q.CATEGORY"></span></div> 
+24
Feb 19 '13 at 19:03
source share

I used that

 function chatSearchCtrl($scope, $http,$sce) { // some more my code // take this data['message'] = $sce.trustAsHtml(data['message']); $scope.searchresults = data; 

and in html i did

 <p class="clsPyType clsChatBoxPadding" ng-bind-html="searchresults.message"></p> 

thats it i get a tag <br/>

+2
Feb 18 '15 at 5:43
source share

Why is it so hard?

I solved this problem just like this:

  <pre>{{existingCategory+thisCategory}}</pre> 

It will automatically do <br /> if the line contains '\ n', which contain when I saved data from textarea.

+2
Jan 11 '16 at 6:57
source share



All Articles