简体   繁体   English

根本无法使AngularJS中的路由正常工作

[英]Cannot get routes in AngularJS to work at all

So I started from scratch and rebuilt it as follows. 因此,我从头开始,并按如下所示对其进行了重建。 Freshly downloaded angularJS (1.5.8) and I've simply deposited both angular.js and angular-route.js in the library folder. 新下载的angularJS(1.5.8),我只是将angular.js和angular-route.js都存储在库文件夹中。 Set up like this: https://gyazo.com/311679bf07249109671d621bc89d2d52 像这样设置: https : //gyazo.com/311679bf07249109109d621bc89d2d52

index.html: 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 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;
})

What's the error you are getting, you need to give more details about it. 您遇到的错误是什么,您需要提供有关它的更多详细信息。 Though you can try, 虽然可以尝试

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

It automatically redirects to blog when no other route is loaded. 当没有其他路由加载时,它将自动重定向到博客。 and also I couldn't find ng-app directive in your html part, you should look into that. 而且我在您的html部分中找不到ng-app指令,您应该调查一下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM