3

Is there any reason this is not working ? :

this.categoriesId = $rootScope.categoriesList.map(function(obj) {
  return obj.id;
}).join('<br />');
this.categoriesName = $rootScope.categoriesList.map(function(obj) {
  return obj.name;
}).join('<br />');

and in the view :

<b>Categories ID :</b><br/><br/>
{{h.categoriesId}}
<hr>
<b>Categories Name :</b><br/><br/>
{{h.categoriesName}}

There is no line breaks, the <br /> isn't interpreted.

How can I work around this ?

Ellone
  • 3,644
  • 12
  • 40
  • 72

3 Answers3

4

Because Angular escapes the HTML in your string before it inserts it into the DOM, in order to prevent XSS attacks.

Messy Fix

Use the ng-bind-html directive to insert the HTML as is.

<span ng-bind-html="h.categoriesName"></span>

Make sure to include the $sanitize service, and ng-bind-html will automatically sanitize your string to make sure it's safe within its context.

Clean Fix

Use ng-repeat to iterate over the list without needing to worry about inserting markup into your string.

<b>Categories Name :</b><br/><br/>
<span ng-repeat="name in categoriesList">
  {{name}}<br />
</span>
Dan Prince
  • 29,491
  • 13
  • 89
  • 120
1

{{}} is only interpreted as text , not html

Use ng-repeat , there is no need to parse array to html

charlietfl
  • 170,828
  • 13
  • 121
  • 150
0

Try this way:

<b>Categories Name :</b><br/><br/>
<span ng-bind-html="h.categoriesName"><span>

Or create map and join filter:

app
.filter('map', function () {
    return function (arr, prop) {
        return arr.map(function (item) {
            return item[prop];
        });
    };
})
.filter('join', function () {
    return function (arr, sep) {
        return arr.join(sep);
    };
});

HTML:

<span ng-bind-html="categoriesList | map: 'name' | join '<br/>'"><span>
karaxuna
  • 26,752
  • 13
  • 82
  • 117