Cannot show information from DRF page using AngularJS

怎甘沉沦 提交于 2019-12-12 02:09:32

问题


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? :(


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/30631195/cannot-show-information-from-drf-page-using-angularjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!