在Django中創建角度頁面以使用來自JSON的數據

[英]Create angular page in Django to consume data from JSON


In an angular controller I have a list of items:

在角度控制器中,我有一個項目列表:

app.controller('MainController', ['$scope', function($scope) { 
    $scope.items = [
                    {"foo":"bar 1"},
                    {"foo":"bar 2"},
                    {"foo":"bar n"}
                   ]
}]);

The following html page, based on angular, displays a list of item:

以下html頁面基於angular,顯示項目列表:

<!DOCTYPE html>
<html>
<head>
<title>list</title>
<script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
</head>
<body data-ng-app="myApp">

<div data-ng-controller="MainController">
  <p>List of items:</p>
  <ul>
    <li data-ng-repeat="item in items">
      {{ item.foo }}
    </li>
  </ul>
</div>

</body>
</html>

How can I integrate that html page in Django and bind it to a Django URL?

如何在Django中集成該html頁面並將其綁定到Django URL?

1 个解决方案

#1


1  

You can add the angular app as a simple template view in Django

您可以將角度應用程序添加為Django中的簡單模板視圖

views.py

views.py

def index(request):
    return render(request, 'yourhtml.html', {})

urls.py

urls.py

....    
url(r'^your_url/$', views.index),
....

Then the index.html file can have your angular code

然后index.html文件可以有你的角度代碼


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2015/06/22/72f7ee8eb4ed4534e62559202e43d320.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com