Kotanet
Kotanet

Reputation: 573

Angularjs convert string to object inside ng-repeat

i've got a string saved in my db

{"first_name":"Alex","last_name":"Hoffman"}

I'm loading it as part of object into scope and then go through it with ng-repeat. The other values in scope are just strings

{"id":"38","fullname":"{\"first_name\":\"Alex\",\"last_name\":\"Hoffman\"}","email":"alex@mail","photo":"img.png"}

But I want to use ng-repeat inside ng-repeat to get first and last name separate

<div ng-repeat="customer in customers">    
    <div class="user-info" ng-repeat="name in customer.fullname">
       {{ name.first_name }} {{ name.last_name }}
    </div>
</div>

And I get nothing. I think, the problem ist, that full name value is a string. Is it possible to convert it to object?

Upvotes: 3

Views: 21074

Answers (4)

Rakesh
Rakesh

Reputation: 329

I had same problem, but i solve this stuff through custom filter...

JSON :

.........
"FARE_CLASS": "[{\"TYPE\":\"Economy\",\"CL\":\"S \"},{\"TYPE\":\"Economy\",\"CL\":\"X \"}]",
.........

UI:

<div class="col-sm-4" ng-repeat="cls in  f.FARE_CLASS | s2o">
   <label>
       <input type="radio"  ng-click="selectedClass(cls.CL)" name="group-class" value={{cls.CL}}/><div>{{cls.CL}}</div>
   </label>
</div>

CUSTOM FILTER::

app.filter("s2o",function () {
    return function (cls) {
        var j = JSON.parse(cls);
        //console.log(j);
        return j;
    }

}); 

Upvotes: 1

Robo Rick
Robo Rick

Reputation: 789

First off... I have no idea why that portion would be stored as a string... but I'm here to save you.

When you first get the data (I'm assuming via $http.get request)... before you store it to $scope.customers... let's do this:

$http.get("Whereever/You/Get/Data.php").success(function(response){
//This is where you will run your for loop
  for (var i = 0, len = response.length; i < len; i++){
    response[i].fullname = JSON.parse(response[i].fullname)
    //This will convert the strings into objects before Ng-Repeat Runs
  //Now we will set customers = to response
   $scope.customers = response

 }
})

Now NG-Repeat was designed to loop through arrays and not objects so your nested NG-Repeat is not necessary... your html should look like this:

<div ng-repeat="customer in customers">    
<div class="user-info">
   {{ customer.fullname.first_name }} {{ customer.fullname.last_name }}
</div>

This should fix your issue :)

Upvotes: 7

Dragos Rusu
Dragos Rusu

Reputation: 1568

My advise is to use a filter like: <div class="user-info"... ng-bind="customer | customerName">...

The filter would look like:

angular.module('myModule').filter('customerName', [function () {
    'use strict';

    return function (customer) {
      // JSON.parse, compute name, foreach strings and return the final string
    };
  }
]);

Upvotes: 1

tymeJV
tymeJV

Reputation: 104775

You'd have to convert the string value to an object (why it's a string, no idea)

.fullname = JSON.parse(data.fullname); //replace data.fullname with actual object

Then use the object ngRepeat syntax ((k, v) in obj):

<div class="user-info" ng-repeat="(nameType, name) in customer.fullname">
    {{nameType}} : {{name}}
</div>

Upvotes: 2

Related Questions