1
我有以下的HTML文件Django中找不到角文件
<html>
<head>
{% load staticfiles %}
<script type="text/javascript" src="{% static "app.js" %}"/></script>
<title>Flapper News</title>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.19/angular.min.js"></script>
</head>
{% verbatim foo %}
<body ng-app="flapperNews" ng-controller="MainCtrl">
<div>
<div ng-repeat="post in posts | orderBy:'-upvotes'">
<span ng-click="incrementUpvotes(post)">^</span>
<a ng-show="post.link" href="{{post.link}}">
{{post.title}}
</a>
<span ng-hide="post.link">
{{post.title}}
</span>
- upvotes: {{post.upvotes}}
</div>
<form ng-submit="addPost()">
<input type="text" placeholder="Title" ng-model="title"></input>
<br>
<input type="text" placeholder="Link" ng-model="link"></input>
<br>
<button type="submit">Post</button>
</form>
</div>
</body>
{% endverbatim foo %}
</html>
這是我angularjs文件
'use strict'
angular.module('flapperNews', ['ngRoute'])
.controller('MainCtrl', [
'$scope',
function($scope){
$scope.test = 'Hello world!';
$scope.posts = [];
$scope.addPost = function(){
if($scope.title === '') { return; }
$scope.posts.push({
title: $scope.title,
link: $scope.link,
upvotes: 0
});
$scope.title = '';
$scope.link = '';
};
$scope.incrementUpvotes = function(post) {
post.upvotes += 1;
};
}]);
模板位於mysite的/模板和角文件位於mysite的/靜態的。當我看看開發服務器請求頁面時,我收到下面的輸出:
[10/Jul/2015 09:23:09]"GET /guides/ HTTP/1.1" 200 861
[10/Jul/2015 09:23:09]"GET /static/app.js HTTP/1.1" 404 1632
當我看看從Firefox開發者控制檯,我收到一個錯誤:[$注射器:modulerr。你們知道爲什麼我的app.js的使用不起作用嗎?
編輯:在settings.py
STATIC_URL = '/static/'
STATIC_ROOT = '/home/ubuntu/mysite/static/'
現在找到該文件,但收到錯誤「angular is not defined」 –
我更新了我的答案,也解釋了不同的靜態設置的作用;) – jgadelange
這是由於您加載app.js '在角度之前。當你把'script'-tag放在''script''標籤後加載'app.js'時,它應該可以工作。 – jgadelange