Itsik Mauyhas
Itsik Mauyhas

Reputation: 4002

How to make a valid JSON work with ng-repeat?

My response from Java servlet via Angular, the requested content is text/html and I used data.split:

d = response.data.replace(/^\s+|\s+$/g, ''); // remove /r/n
data = d.split(" ");
for(var i =0 ; i<data.length; i++){
  data[i] = '{' + data[i] + '}'; // add {} to each k.v
}

The result looks like this:

["{key:myKey,value:true}", "{key:myKey,value:true}"....]

And my HTML

<ul>
  <li ng-repeat="line in fixedDBArray">
    {{line.key}} - {{line.value}} 
  </li>
</ul>

The Angular data-binding looks like:

$scope.fixedDBArray = data //response.data

And {{fixedDBArray}} works fine but {{line.key}} and {{line.value}} do not work. I checked http://jsonlint.com/ and the JSON is valid. Does anyone know what the problem is?

Upvotes: 0

Views: 64

Answers (1)

Pavan Teja
Pavan Teja

Reputation: 3202

If you still want to fix this as it stands now. you can use replace method and make the value valid JSON object. i made a sample implementation of this here

make sure that you use more efficient regular expression for adding additional quotes.just posting it for your reference without considering performance or complexity.

Upvotes: 1

Related Questions