Cannot show information from DRF page using Angula

2019-08-28 21:53发布

Here are the codes that I've written:

DRF Page [ http://localhost:8000/index/info/?format=json ]

[{"id": 1, "name": "Michel", "city": "Florida", "country": "United States"}, {"id": 2, "name": "Shuvo", "city": "London", "country": "United Kingdom"}]

2.html [this is my second attempt]

<!DOCTYPE html>

<html>
<script src="angular.min.js"></script>
<script src="2.js"></script>
<body>
<div ng-app="myApp" ng-controller="myCtrl">
    <ul>
        <li ng-repeat="x in info">
            {{ x.name + ', ' + x.country }}
        </li>
    </ul>
</div>
</body>    
</html>

2.js

var app = angular.module('myApp', []);

app.controller('myCtrl', function($scope, $http) {
    $http.get("http://localhost:8000/index/info/?format=json")
    .success(function(response) {
        $scope.info = response;
    });
});

My 2.html page shows NOTHING. It's completely blank. What am I doing wrong? :(

2条回答
【Aperson】
2楼-- · 2019-08-28 22:03

I suspect it's something to do with the format of the data being returned in

$http.get("http://localhost:8000/index/info/?format=json")

What are you seeing in the console when http.get runs?

Here's a plunkr showing it working with your data

http://plnkr.co/edit/x0OtVbsnSMk3mdhRFEkc

查看更多
孤傲高冷的网名
3楼-- · 2019-08-28 22:09

since django and angular use the same notation to display variables, you will have to use the verbatim tags for using {{}} as angular tags. Otherwise they will be treated as django tags.

查看更多
登录 后发表回答