无法在AngularJS中获取路由

时间:2016-08-21 02:20:53

标签: javascript html angularjs

所以我从头开始并按如下方式重建它。新下载的angularJS(1.5.8)和我只是将angular.js和angular-route.js存放在库文件夹中。设置如下:https://gyazo.com/311679bf07249109671d621bc89d2d52

的index.html:

<!DOCTYPE html>
  <html ng-app="myApp">
    <head>
      <meta charset="utf-8">
      <title>TITLE</title>
      <link rel="stylesheet" type="text/css" href="css/main.css" charset="utf-8">
   </head>
<body ng-controller="MainController">

<div class="header">
  <a href="index.html"><h1>Alice Birt Photography</h1></a>
</div>

<div class="navbar">
  <ul>
    <li><a href="#/about">About Me</a></li>
    <li><a href="#/photo">My Photography</a></li>
    <li><a href="#/blog">My Blog</a></li>
  </ul>
</div>

<div ng-view></div>

<div class="footer">
  <ul>
    <li><a href="">Facebook</a></li>
  </ul>
</div>

<!-- JS-Library -->
<script type="text/javascript" src="library/angular.js"></script>
<script type="text/javascript" src="library/angular-route.js"></script>

<!-- JavaScript -->
<script type="text/javascript" src="js/main.js"></script>
<script type="text/javascript" src="js/app.js"></script>

app.js

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

app.config(function ($routeProvider) {
  $routeProvider
    .when('/blog', {
      controller: 'BlogController',
      templateUrl: 'page/blog.html'
    })
    .when('/photo', {
      controller: 'PhotoController',
      templateUrl: 'page/photography.html'
    })
    .when('/about', {
      controller: 'AboutmeController',
      templateUrl: 'page/aboutme.html'
    })
});

app.controller('MainController', function($scope, $route, $routeParams, $location) {
  $scope.$route = $route;
  $scope.$location = $location;
  $scope.$routeParams = $routeParams;
})

app.controller('BlogController', function($scope, $routeParams) {
  $scope.name = 'BlogController';
  $scope.params = $routeParams;
})

app.controller('PhotoController', function($scope, $routeParams) {
  $scope.name = 'PhotoController';
  $scope.params = $routeParams;
})

app.controller('AboutmeController', function($scope, $routeParams) {
  $scope.name = 'AboutmeController';
  $scope.params = $routeParams;
})

1 个答案:

答案 0 :(得分:0)

您得到的错误是什么,您需要提供更多详细信息。虽然你可以试试,

$routerProvider.otherwise(redirectTo: '/blog');

当没有加载其他路由时,它会自动重定向到博客。而且我在你的html部分找不到 ng-app 指令,你应该研究一下。