Imran Imran - 11 months ago 52
Javascript Question

How to read parsed JSON from text in angular view

I need to read the following JSON text from my fetched database, below was my sample code:


The real data in

[{left:'Benda Hidup',right:'Benda Mati'}] //Fetched from database and as text

When I tried to read back, the output was only
. How to solve this problem?

Answer Source

Since the fetched value from the database is simple text, use JSON.parse to parse into a json object and retrieve it.

[{left:'Benda Hidup',right:'Benda Mati'}] this is a string, so returning [ as first value.

Method1: just return the JSON parsed value from the contrller,

In your controller:

    $scope.qst_cnt_options =  $scope.questionData1[0].JSON.parse(qst_cnt_options)

In your view:



    $scope.qst_cnt_options =  questionData1[0].JSON.parse(qst_cnt_options)

    $scope.parJson = function (json) {
            return JSON.parse(json)[0];