2016-07-14 41 views
1

我正在構建一個簡單的角度前端頁面來顯示一些簡單的html頁面,並試圖讓angular-route正常工作。我在頂部有一個導航欄,我希望在其下面顯示一個視圖,以便在該視圖內更改html頁面。我有一個routing.js文件,控制器和路由信息是從主html頁面引用的。我的問題是,我的觀點沒有被顯示。當我檢查網頁控制檯以查看未顯示視圖的原因時,它將返回404 Not Found。我不知道爲什麼考慮我在根目錄下有routing.js文件。這是我的index.html頁面,包含導航欄和視圖應該顯示的位置。使用Angular-route找不到路由javascript文件

<!DOCTYPE html> 


<html> 

    <head> 
    <meta charset="utf-8"> 
    <meta http-equiv="X-UA-Compatible" content="IE=edge"> 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags --> 
    <meta name="description" content=""> 
    <meta name="author" content=""> 
    <link rel="icon" href="../../favicon.ico"> 

    <title>Register License Plate</title> 

    <!-- Libraries --> 


<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous"> 

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script> 

<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> 
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-route.js"></script> 

<!-- to get routing information and controllers --> 
<script src="routing.js"></script> 

</head> 

<body> 

<div ng-app = "plateApp" ng-controller = "defaultController"> 

<!-- NAV BAR --> 
<nav class="navbar navbar-default"> 
    <div class="container-fluid"> 
    <!-- Brand and toggle get grouped for better mobile display --> 
    <div class="navbar-header"> 
     <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false"> 
     <span class="sr-only">Toggle navigation</span> 
     <span class="icon-bar"></span> 
     <span class="icon-bar"></span> 
     <span class="icon-bar"></span> 
     </button> 
     <a class="navbar-brand" a href = "/">LPR</a> 
    </div> 

    <!-- Collect the nav links, forms, and other content for toggling --> 
    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1"> 
     <ul class="nav navbar-nav"> 
     <li class="active"><a href="#register">Register License Plate <span class="sr-only">(current)</span></a></li> 
     <li><a href="#payticket">Pay Ticket</a></li> 
     </ul> 
     <form class="navbar-form navbar-right" role="search"> 
     <div class="form-group"> 
      <input type="text" class="form-control" placeholder="Enter Lot Number"> 
     </div> 
     <button type="submit" class="btn btn-default">Submit</button> 
     </form> 
     <form class="navbar-form navbar-right" role="search"> 
     <button type="submit" class="btn btn-default">Submit</button> 
     <div class="form-group"> 
      <input type="text" class="form-control" placeholder="Enter Plate Number"> 
     </div> 
     </form> 
    </div><!-- /.navbar-collapse --> 
    </div><!-- /.container-fluid --> 
</nav> 

</div> 


<!-- Show views here --> 
<div id="main"> 

<div ng-view></div> 

</div> 

<!-- Test words to show something is being displayed after the view is displayed --> 
After View is Displayed 


    </body> 

</html> 

考慮到我引用了routing.js腳本,我很茫然。我是新來的角js,所以我顯然做錯了什麼。下面是我的routing.js腳本。

var plateApp = angular.module('plateApp', ['ngRoute']); 

plateApp.config(function($routeProvider) { 
    $routeProvider 

    // route for default/welcome page 
    .when('/', { 
     templateUrl : 'default.html', 
     controller : 'defaultController' 

    }) 
    // route for Register LP Page 
    .when('/register', { 
     templateUrl : 'register.html', 
     controller : 'registerController' 

    }) 

    // router for Paying Tickets page 
    .when('/payticket', { 
     templateUrl : 'payticket.html', 
     controller : 'payticketController' 
    }) 

    // route for getting plates and Lot page 
    .when('/getplates', { 
     templateUrl : 'getplates.html', 
     controller : 'getPlatesController' 
    }); 
}); 

//Controllers 
plateApp.controller('defaultController', function($scope){ 
    //Keeps scopes and other Vars here 
    $scope.testMessage = 'Welcome to this website'; 
}); 

plateApp.controller('registerController', function($scope){ 
    //Keeps scopes and other Vars here 
    $scope.testMessage = 'Register your plates'; 
}); 

plateApp.controller('payticketController', function($scope){ 
    //Keeps scopes and other Vars here 
    $scope.testMessage = 'Pay Tickets'; 
}); 

plateApp.controller('getPlatesController', function($scope){ 
    //Keeps scopes and other Vars here 
    $scope.testMessage = 'Get Plates from SQLdb'; 
}); 

還有一些有關如何運行服務器的背景信息。我有一個Linux機器運行節點,並在端口1337上運行

當我輸入192.168.239.130:1337/服務器的地址時,我將它發送回index.html頁面。這加載罰款,我能夠看到導航欄/網頁,除了我沒有看到其他HTML頁面的意見加載,因爲顯然沒有找到routing.js文件。

任何幫助,將不勝感激。

感謝。

+0

是index.html還在根?如果沒有,你可能想把你的腳本標記改成''確定它在網站根目錄 – kennasoft

+0

href's不正確,#'應該像''後面'' :'href =「#/ register」' – charlietfl

+0

你的意見是在一個子文件夾中,還是在根目錄和routing.js?另外,我看到你正在將ng-app屬性放在一個div中,該div不包含具有ng-view屬性的div。你應該將ng-app移動到body標籤。 – morecchia808

回答

1

看起來像模板URL的問題。確保您提供的作爲模板URL的路徑是相對URL。

因此,例如,如果您的視圖模板(HTML)存在於名爲「視圖」的文件夾中,那麼您的模板網址應該是'/views/default.html',前提是'視圖'文件夾位於根級。

+0

出於測試目的,我已將所有html和javascript文件當前放在同一個根目錄中。 – HardLuckHank