In my MySQL database I have column with json string like this:

[{id:1,name:"VEZAN ZA PROJEKT 1"}, {id:1, name:'VEZAN ZA PROJEKAT 2'}]

I am trying to display values inside template like this:

<span ng-repeat="(key, val) in singletask.depencies">
    {{val}}
</span>

But get error:

Duplicates in a repeater are not allowed. Use 'track by' expression to specify unique keys. Repeater: (key, val) in singletask.depencies, Duplicate key: string::, Duplicate value: :

If I add track by $index it will display full json sting separated with spaces...

Anyone know what is problem?

EDIT

I pull JSON from array of object. Here is how my objects look like:

{
    tasks : [
        0 : Object
        1 : Object
    ],
    admin_id : 1,
    created_at : "2015-11-02 16:16:27",
    depencies : "[{id:1,name:" VEZAN ZA PROJEKT 1 "}, {id:1, name:'VEZAN ZA PROJEKAT 2'}]"
}
share|improve this question
up vote 1 down vote accepted

Your data is coming back as a string from the DB, not as a JavaScript object. To convert it you can use something like https://docs.angularjs.org/api/ng/function/angular.fromJson

If you still have problems, post more of your code.

Edit: In your controller, after you have received your data, you need to run a foreach loop on your collection of tasks (or if you only have one task, just do it once) to change the string object into a JavaScript object.

$http.get("/api/data").then(function(result){
    $scope.singletask = result.data

    angular.forEach($scope.singletask, function(value, key) {
        value.dependencies = angular.fromJson(value.dependencies);
    }
}

Then, when you go to the ng-repeat="(key, val) in singletask.dependencies" it will iterate over the array object.

share|improve this answer
    
I also tought about that.... But problem is my json is inside object... So I need inside template to convert that json string... – Vladimir Djukic Nov 2 '15 at 14:33
    
So, what you're saying is that you're getting a JavaScript object back, but it's something like {'data': [{id:1,name:"VEZAN ZA PROJEKT 1"}, {id:1, name:'VEZAN ZA PROJEKAT 2'}]}? If you can, please post some more info about what you're getting, and what you are expecting. – Ron Brogan Nov 2 '15 at 14:36
    
I updated main post.... I get array of object and I in agngular template take them with for each... But also I need make one more foreach inside that for each to take all depencies and depencies is json string... I expect to display every depency name,,, – Vladimir Djukic Nov 2 '15 at 14:45
    
Check out my edited answer. Let me know how it goes. – Ron Brogan Nov 2 '15 at 14:56
    
foreach will slow code execution is there any filter to do that directly in template? – Vladimir Djukic Nov 2 '15 at 14:58

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.