0

I'm setting up an Angular JS app to consume a Django REST API, and I'm stuck on the rendering of data.
I already asked another question (this), but the solution that I've been given doesn't work.

I was wondering that maybe there is something wrong in the API view, could it be a problem when trying to render the data from the Angular controller?

Anyway, this is my Angular app + template (edited as suggested in the other stackoverflow question)

base.html

<body ng-app="schoolApp" ng-controller="schoolCtrl as vm">
    <p>Hello {{vm.name}}!</p>
    <div>
        <table class="table table-striped">
            <thead>
                <tr>
                    <th>Classroom</th>
                    <th>School</th>
                    <th>Floor</th>
                    <th>Academic year</th>
                </tr>
            </thead>
            <tbody>
                <tr ng-repeat="classroom in vm.classrooms">
                    <td>{{classroom.classroom}}</td>
                    <td>{{classroom.school.school_name}}</td>
                    <td>{{classroom.floor}}</td>
                    <td>{{classroom.academic_year}}</td>
                </tr>
            </tbody>
        </table>
    </div>
</body>

app.js

var schoolApp = angular.module('schoolApp', ['ngResource']);

schoolApp.factory('Classroom', ['$resource', function($resource) {
    return $resource('/classrooms/?format=json', {}, {
        query: {
            method: 'GET',
            isArray: true,
        }
    });
}]);

schoolApp.controller('schoolCtrl', function($scope, Classroom) {
  var vm = this;
  vm.name = 'World';
   Classroom.query().$promise.then(function(data) {
     console.log('Success: '+JSON.stringify(data));
     vm.classrooms = data;

   }, function (reason) {
     console.log('ERROR: '+JSON.stringify(reason));
   });
});

I was thinking that maybe there is some problem on the view, so here's the REST API view

class HomePageView(TemplateView):
    template_name = 'school_app/base.html'

class StudentViewSet(viewsets.ModelViewSet):
    queryset = Student.objects.all()
    serializer_class = StudentSerializer


class ClassroomViewSet(viewsets.ModelViewSet):
    queryset = Classroom.objects.all()
    serializer_class = ClassroomSerializer

What am I doing wrong?

UPDATE:

This is what I get

enter image description here

Data on the console: yes.
Data on the tables: no.

2 Answers 2

1

As you are using controllerAs syntax, you have binded all data to controller context. So you could get data on view using its alias like vm.classrooms

<tr ng-repeat="classroom in vm.classrooms">
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, that was my omission (the guy that gave me the solution on the other question specified that). However, it's not working anyway. I'm updating the question to show a screenshot of what I get.
0

Have you tried to output in the html the content of the variable vm.classrooms.

You can do it with

<pre>{{vm.classrooms|json}}</pre>

It seems to me that you are not binding correctly the variables inside the table,

i can't see it clearly in the picture, it can be {{ classroom.school.academic_year }} ??

7 Comments

The variables are binded correctly, given that I tried to build an Angular app alone with hardcoded Json data (taken from the Django REST data), and the tables are filled correctly. Anyway, I'm trying that, but I get an error: "Invalid filter: 'json'"
I do not understand the error, jsonfilter is part of angular built in filters.
Any way if you have tried with harcoded data and it works, the problem is in the call back from the query , have you checked what data is.
I've tried without the filter, and the template doesn't receive the data. What do you mean with "what data is"? It's json data... is this your doubt?
Apologize my english is not as good as i would like. I mean in your callback function you have a variable called data, which you already are showing in the console in your post, so forget my comment.
|

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.