angularjs - ngRoute无法正常工作

时间:2015-12-28 16:29:46

标签: javascript angularjs ngroute

ngRoute以前工作正常并且现在停止工作,但是添加了一些文件和控制器。 在浏览器中,我将网址设为 http://localhost/#browsefp ,而不是 http://localhost/#/browsefp

下面是我的代码,请帮忙。学习AngularJS并不断遇到奇怪的问题。在JS控制台中没有看到错误。

app.js

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

 app.config(function($routeProvider){
 $routeProvider 
   .when('/', {templateUrl : 'views/main.html', controller : 'mainController' })
   .when('/addnew', {templateUrl : 'views/addnew.html', controller : 'homeController', css : 'css/screen.css'})
   .when('/addnewfp', {templateUrl : 'views/addnewfeeprogram.html', controller : 'homeController', css : 'css/screen.css'})
   .when('/addnewcm', {templateUrl : 'views/addnewcustomermapping.html', controller : 'aboutController', css : 'css/screen.css'})
.when('/browsefp', {templateUrl : 'views/browseprogram.html', controller : 'browseprogramController', css : 'css/screen.css'})
.otherwise({ redirectTo : '/' })
 }); 

index.html

<!doctype html>
<!-- define angular app -->
<html ng-app="DevStreamApp">
<head>
<titleFee </title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<style type="text/css" media="screen">
   @import url("/css/screen.css");
   @import url("/js/yui/container.css");
</style>

<!-- load angular and angular route via CDN -->
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.25/angular.min.js">        </script>
  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.25/angular-route.js"></script>
  <script src="js/app.js"></script>
</head>
<body>
<hr noshade/>
 <!-- MAIN CONTENT AND INJECTED VIEWS -->
    <div id="main">
        <!-- angular templating -->
        <!-- this is where content will be injected -->
        <a href="#addnew">Add new </a><br/>  
        <a href="#addnewfp">Add new Fee Program</a><br/>  
        <a href="#addnewcm">Add new Customer mapping</a><br/> 
        <a href="#browsefp">Browse Fee Program</a>
        <div ng-view></div>
    </div>
</body>
<script src="js/app.js"></script>
<script src="js/controllers/mainController.js"></script>
<script src="js/controllers/homeController.js"></script>
<script src="js/controllers/aboutController.js"></script>
<script src="js/controllers/contactController.js"></script>
<script src="js/controllers/browseprogramController.js"></script>
</html>

mainController.js

 //create the controller module 
 angular.module('DevStreamApp').controller('mainController', function($scope) {

// create a message to display in our view
$scope.message = 'Everyone come in Main Controller!';
});

2 个答案:

答案 0 :(得分:1)

从href中删除#,正如您在doc中看到的那样,他们将其配置如下:

<a href="Book/Moby">Moby</a>

.when('/Book/:bookId', {
      ....
});

所以在你的情况下,它将是:

    <a href="addnew">Add new </a><br/>  
    <a href="addnewfp">Add new Fee Program</a><br/>  
    <a href="addnewcm">Add new Customer mapping</a><br/> 
    <a href="browsefp">Browse Fee Program</a>

答案 1 :(得分:0)

默认情况下,AngularJS将使用主题标签路由URL,但您可以使用$ locationProvider将其删除。

您将使用$ locationProvider模块并将html5Mode设置为true。

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

 app.config(function($routeProvider,  $locationProvider){
     $locationProvider.html5Mode(true);

     ... your code ...

在index.html中,链接必须是

<a href="addnew">Add new </a><br/>