0

I am new to angularjs and trying my hands on a an application. I am trying to create load more feature and stuck with .push() to the scope element.

How to push posts in following data to a $scope variable? Please check the code in controller below.

{
"status": 200,
"result": {
    "success": true,
    "message": [
        {
            "offset": 6,
            "load_more": true,
            "brand_name": "HARLEY DAVIDSON",
            "posts": [
                {
                    "postID": 41,
                    "post_title": "DYNA STREET BOB (FD2) (FXDB)",
                    "engine_size": "1584 ccm",
                    "year": "2007-2013"
                },
                {
                    "postID": 43,
                    "post_title": "DYNA FAT BOB (FXDF)",
                    "engine_size": "1584 ccm ",
                    "year": "2008-2012"
                },
                {
                    "postID": 46,
                    "post_title": "Bike 1",
                    "engine_size": "1584 ccm ",
                    "year": "2008-2012"
                },
                {
                    "postID": 47,
                    "post_title": "Bike 2",
                    "engine_size": "1584 ccm ",
                    "year": "2008-2012"
                },
                {
                    "postID": 48,
                    "post_title": "Bike 3",
                    "engine_size": "1584 ccm ",
                    "year": "2008-2012"
                },
                {
                    "postID": 49,
                    "post_title": "Bike 4",
                    "engine_size": "1584 ccm ",
                    "year": "2008-2012"
                }
            ]
        }
    ]
},
"timestamp": 1396896763

}

Here's the code in controller:

$scope.bikes = [];

Restangular.all('bikes/getBikesByTermId/' + $stateParams.termID+'/'+$scope.offset).getList().then(function (data) {
    $scope.term_title = data[0].brand_name;
    $scope.bikes = data[0].posts;
    $scope.offset = data[0].offset;
    $scope.load_more = data[0].load_more;
    $scope.isLoading = 0;
});

$scope.loadMore = function(){
    Restangular.all('bikes/getBikesByTermId/' + $stateParams.termID+'/'+$scope.offset).getList().then(function (data) {
        $scope.term_title = data[0].brand_name;
        $scope.bikes.push(data[0].posts);
        $scope.offset = data[0].offset;
        $scope.load_more = data[0].load_more;
        $scope.isLoading = 0;
    });
};
Mohit Aneja
  • 428
  • 4
  • 11
  • I'd suggest you move your restangular calls to a service this way you can store the data within the singleton and can inject it into any controllers that need to use the data. In the controller you can just set the services data onto the model you store on a scope property. – shaunhusain Apr 07 '14 at 19:09
  • This is not an angular problem, it's a simple JavaScript question: [push array values into another array](http://stackoverflow.com/questions/4156101/javascript-push-array-values-into-another-array). And in this case you can even do `$scope.bikes = data[0].posts`, since you probably don't need to create another array – Paolo Moretti Apr 07 '14 at 19:09
  • Thanks @Paolo Moretti, the solution in the link you provided works. You saved my day ;) – Mohit Aneja Apr 07 '14 at 19:27

0 Answers0