Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

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>
share|improve this question
up vote 2 down vote accepted

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');
});
share|improve this answer
    
thanks, and I forgot to ask one more. How do I display the $scope.title inside my html file? I have tried <td ng-repeat="x in title"> {{x}} </td> but it doesn't work – Spadaboyz 14 hours ago
    
should work as shown ...any errors in console? What does happen? – charlietfl 14 hours ago
    
no error shown in console, and this is really a weird one... using console.log($scope.title); showing the result in console but no result shown in html – Spadaboyz 14 hours ago
    
i have edited my question, you can see my angular code and html code there – Spadaboyz 14 hours ago
    
i have found the problem. it seems that i have to put the $scope.title outside Parse.Cloud.run(). Is it possible to put $scope.title array inside another $scope? – Spadaboyz 13 hours ago

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.