1

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'}]"
}

1 Answer 1

1

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.

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

8 Comments

I also tought about that.... But problem is my json is inside object... So I need inside template to convert that json string...
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.
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,,,
Check out my edited answer. Let me know how it goes.
foreach will slow code execution is there any filter to do that directly in template?
|

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.