0

I have an angular app that looks like this:

(function (){
    var app = angular.module('world', []);

    app.controller('WorldController', function(){
        this.tabs = tabs; 
    });

    var tabs =[
    {   
        id: "1",
        name: "first tab name",
        title: "first tab title",
        content: "first tab cont"
    },
    {   
        id: "2",
        name: "orci, consectetuer",
        title: "sem elit, pharetra ut,",
        content: "augue scelerisque mollis. Phasellus"
    },
    {   
        id: "3",
        name: "rhoncus. Nullam velit dui, semper et, lacinia vitae,",
        title: "Nam porttitor",
        content: "tellus eu augue porttitor interdum. Sed auctor odio a purus."
    },
    {   
        id: "4",
        name: "nulla magna, malesuada vel, convallis in, cursus et, eros. Proin",
        title: "montes, nascetur ridiculus mus.",
        content: "Sed eget lacus. Mauris non dui nec urna suscipit nonummy."
    }
    ];

})();

I want to load the whole tabs array from a json. I have no problem to generate a json and fetch it, but I don't know how to convert it into the format as shown above.

When I try to do var tabs=jQuery.getJSON('myJson.json');, the json is fetched, but it's not loaded in the format that I want.

How can I convert a json into the format of the array shown above?

While my json looks like that -

[
    {   
        "id": "1",
        "name": "mus. Proin vel arcu eu odio tristique pharetra.",
        "title": "faucibus",
        "content": "Donec dignissim magna a tortor. Nunc commodo auctor"
    },
    {   
        "id": "2",
        "name": "orci, consectetuer",
        "title": "sem elit, pharetra ut,",
        "content": "augue scelerisque mollis. Phasellus"
    },
    {   
        "id": "3",
        "name": "rhoncus. Nullam velit dui, semper et, lacinia vitae,",
        "title": "Nam porttitor",
        "content": "tellus eu augue porttitor interdum. Sed auctor odio a purus."
    },
    {   
        "id": "4",
        "name": "nulla magna, malesuada vel, convallis in, cursus et, eros. Proin",
        "title": "montes, nascetur ridiculus mus.",
        "content": "Sed eget lacus. Mauris non dui nec urna suscipit nonummy."
    }
];
7
  • That depends on what your json format is Commented Jan 21, 2015 at 10:17
  • I can format it however I want. For now, each element in the json looks like that { "id": "1", "name": "first tab name", "title": "first tab title", "content": "first tab cont" }, Commented Jan 21, 2015 at 10:20
  • You need to provide the format of your json... JSON is not a standard format, the array you have provided can be considered a json. If you don't provide the json format that you fetched, we can't possibly help you transform it into the array format you require... Commented Jan 21, 2015 at 10:24
  • Just edited my question again with the json that I have Commented Jan 21, 2015 at 10:28
  • They are the same format... the only difference is you have written the keys with quotation marks where as your array doesn't have quotation marks.. it really makes no difference to the javascript engine. Commented Jan 21, 2015 at 10:31

1 Answer 1

1

Use the $http service in angular. For example, if you have a data.json file in the same folder that contains the json data then use

app.controller('MainCtrl', function($scope, $http) {
  $http.get('data.json').success(function(data){
    $scope.fetched = data;
  })
});

Here is a plunker example.

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

2 Comments

then just change it to ... var tabs = data;
that's right! but one of the things we should be aware of is that the json should not end with a ;

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.