0

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>
0

1 Answer 1

2

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');
});
Sign up to request clarification or add additional context in comments.

6 Comments

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
should work as shown ...any errors in console? What does happen?
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
i have edited my question, you can see my angular code and html code there
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?
|

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.