0

I want to show a repeated list, and render each element with ngSanitize as follows:

<div ng-repeat="todo in todos" ng-bind-html="todo.text"/>

I get no error in the console, but I also don't get any output. Why?

By the way: the following does actually work, but unformatted markup:

<div ng-repeat="todo in todos">{{todo.text}}</div>
7
  • 1
    Should it be todos and not totos? Also, close the quotes on your ng-repeat. Commented Aug 7, 2015 at 15:09
  • just a typo here, sorry. Commented Aug 7, 2015 at 15:10
  • Why ng-bind-html and not just ng-bind ? Commented Aug 7, 2015 at 15:11
  • Because the text contains html markup elements like <strong>test text</strong>. docs.angularjs.org/api/ng/directive/ngBindHtml Commented Aug 7, 2015 at 15:12
  • What version of angular are you using? Commented Aug 7, 2015 at 15:13

2 Answers 2

3

I think there were a few mistakes in your code. The example below should help you further. Remember, Angular doesn't support self closing <div>-tags.

HTML

<div ng-app="myApp" ng-controller="myAppCtrl">
    <div ng-repeat="todo in totos" ng-bind="todo.text"></div>
</div>

JS

angular.module("myApp", [])
.controller("myAppCtrl", function($scope) {
    $scope.todos = [
        {name: "todo1", text: "This is test todo 1"},
        {name: "todo2", text: "This is test todo 2"},
        {name: "todo3", text: "This is test todo 3"}
    ]
});

JSFiddle: https://jsfiddle.net/ABr/38foxe31/

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

2 Comments

"Remember a <div>-tag is not self closing" Except in XHTML, but very few people use XHTML anymore...
That works. So, if I got you right, I'm not permitted to close like <div .../>, but must use the explicit tag again like <div ...></div> even if the text between the divs is empty?
1

I have created a plunker and tried to replicate your problem, but everything is working fine.

A couple of things to consider:

1) Have you loaded angular-sanitize in your index file? (before app.js)

<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.3/angular-sanitize.js"></script>    

2) Have you correctly injected ngSanitize in your module?

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

1 Comment

Thanks for your hints, which are all correct. Anyways as written above, it turned out I have to explicit close the </div> tag.

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.