Y
Y
Yuri2014-12-10 19:41:14
Angular
Yuri, 2014-12-10 19:41:14

Solve the problem with the list of objects?

Started learning AngularJS, ran into a problem.
I'm trying to display a list of students that I receive from the server in json.
for page navigation in the table I use smart-table
Controller:

studentsControllers.controller('StudentsListCtrl', ['$scope', 'Student',
    function($scope, Student) {
        $scope.itemsByPage=3;
        $scope.studentsCollection = Student.query();
        console.log($scope.studentsCollection);
    }]);

This is how I get json
studentsServices.factory('Student', ['$resource',
    function($resource){
        return $resource(':studentId.json', {}, {
            query: {method:'GET', params:{studentId:'all'}, isArray:true}
        });
    }]);

json file from server:
[
    {
        "id":1,
        "fio":"Иванов Иван Иванович",
        "birthday":"10.08.1983",
        "phone":"34563463",
        "email":"[email protected]",
        "image":"",
        "is_active":1
    },
    {
        "id":2,
        "fio":"Смирнов Михаил Петрович",
        "birthday":"25.08.1983",
        "phone":"73263463465",
        "email":"[email protected]",
        "image":"",
        "is_active":1
    }
]

The problem is that it is not a list of objects that comes in, if you insert the contents of the json file into the $scope.studentsCollection variable, then the code works without problems, the data is displayed normally.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Denis, 2014-12-11
@cjbars

try like this

// вместо
 $scope.studentsCollection = Student.query();
// сделать
Student.query().success(function(response){
 $scope.studentsCollection = respose;
})

M
Mikhail Osher, 2014-12-10
@miraage

I strongly recommend using Restangular for CRUD operations .
Very cool library.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question