Ad

Put Parse.com JSON Objects Into Angular $scope Array

- 1 answer

I want to put Parse.com JSON object into angular $scope, but my code seems not working
I want to get 'title' from the 'objects' and put it as array in $scope.title

And this is some part of my js code:

main_app.controller('getList', function($scope) {
Parse.Cloud.run("MJSEvent_All",{}, {
    success: function(results) {

        var object = results['objects'];
        for (i = 0; i < object.length; i++) {
            $scope.title = [object[i].get('title')];
        };
    },
    error: function(errorObj) {
        console.log(errorObj);
    }
}); });

and for the html view:

<div class="row" ng-app="getParse" ng-controller="getList">
    <h3>Event List</h3>
    <table >
        <tr>
            <th>Title</th>
            <th>Speaker</th>
        </tr>
        <tr>
            <td ng-repeat="x in title"> {{x}} </td>
        </tr>
    </table>
</div>
Ad

Answer

You are over writing the same variable $scope.title each iteration of the loop, so it will only end up being the last title in array

If you want an array of titles $scope.title needs to be an array

$scope.title=[];
var object = results['objects'];
for (i = 0; i < object.length; i++) {
    $scope.title.push( [object[i].get('title')]);
};

Or use map()

$scope.title = results['objects'].map(function(item){
     return item.get('title');
});
Ad
source: stackoverflow.com
Ad