Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have 2 json files,services.json and services_show.json.At page load am fetching the data from services.json and it working properly.On a button click,i need to fetch the contents from service_show.json and append to the services array but it does not work.

var beautyApp = angular.module('findbeauty', []);

beautyApp.controller('beautycntrl',function($scope,$http){

    $http.get('http://localhost/Find-Beauty/media/services.json').success(function(data) {
        $scope.services=data.services;
        $scope.services1=data.services1;
    });

    $scope.Add = function(){

        $http.get('http://localhost/Find-Beauty/media/services_show.json').success(function(data) {
            console.log(angular.toJson(data.services));
            $scope.services.push(data.services);

        });

    };

    $scope.ViewMore = function(){

});

Services.json

{
"services":[
{
            "name": "Arun",
            "gender": "Damen",
            "duration": "1.5 Stunden",
            "price": "€65,00",
            "imagepath": "media/images/prfilepic1.png",
            "percentage": "90%"
        },

    ],
    "services1":[

    {
            "name": "Schnitt & Föhnen",
            "gender": "Damen",
            "duration": "1.5 Stunden",
            "price": "€65,00",
            "imagepath": "media/images/profilepic4.png",
            "percentage": "25%"
        },


    ]
}

service_show.json

{
"services":[
{
                "name": "Schnitt & Föhnen",
                "gender": "Damen",
                "duration": "1.5 Stunden",
                "price": "€65,00",
                "imagepath": "media/images/profilepic4.png",
                "percentage": "5%"
            },

    ],
    "services1":[

    {
                "name": "Schnitt & Föhnen",
                "gender": "Damen",
                "duration": "1.5 Stunden",
                "price": "€65,00",
                "imagepath": "media/images/prfilepic1.png",
                "percentage": "50%"
            },

    ]
}

How can i push the services_show.json data to $scope.services ? Any Help?

share|improve this question
 
What do you mean by it doesn't work? You are missing to close your controller so that code won't work no matter what. Look here for the correct code: gist.github.com/VictorBjelkholm/8b2004289ed9b4336034 –  Victor Bjelkholm Sep 26 at 9:43
 
Tried running your code locally and it is working fine for me.. –  user700284 Sep 26 at 9:49
 
Thanks all for the reply.. –  Gopesh Sep 26 at 10:33
add comment

2 Answers

up vote 1 down vote accepted

Try this once

$scope.services.push.apply($scope.services, data.services);
share|improve this answer
 
Thanks ..It worked.. –  Gopesh Sep 26 at 10:33
add comment

You need to push one item at a time, like

angular.forEach(data.services,function(item) {
     $scope.services.push(item);
});
share|improve this answer
add comment

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.