无法使AngularJS路由工作

时间:2018-03-04 18:49:20

标签: javascript angularjs routing wampserver

我确信我有一些简单的东西,但是我似乎无法找到它。我有一个超级简单的Angular JS SPA。文件夹结构如下:

的index.php
- js
- app.js
- 页面 - home.html
- post.html
- know.html

index.php中的

我有:

<html class="no-js" lang="en" ng-app="postApp">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="x-ua-compatible" content="ie=edge">
        <title>Post Safety App</title>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <base href="/postsafetyapp/">
    </head>
    <body ng-controller="mainController">
        <main id="main">
            <div ng-view></div>
        </main>

        <!-- Angular JS -->
        <script src="js/vendor/angular.min.js"></script>  
        <script src="js/vendor/angular-route.min.js"></script>        

        <!-- Custom JS -->
        <script src="js/app.js"></script> 
    </body>
</html>
在home.html中我有:

<div class="home buttons">
    <div class="button button-1-of-2">
        <a href="#share" class="button__link" id="button_share">
            <span class="button__icon"><i class="fa fa-share"></i></span>
            <span class="button__text">SHARE</span>
        </a>
    </div>
    <div class="button button-1-of-2">
        <a href="#know" class="button__link" id="button_share">
            <span class="button__icon"><i class="fa fa-graduation-cap"></i></span>
            <span class="button__text">KNOW</span>
        </a>
    </div>    
</div>

以下是app.js中的代码:

// create the main module
var postApp = angular.module("postApp", ['ngRoute']);

// configure our routes
postApp.config(function($routeProvider, $locationProvider) {

    $routeProvider

         // route for the home page
        .when('/', {
            templateUrl : 'pages/home.html',
            controller  : 'mainController'
        })

        // route for the know page
        .when('/know', {
            templateUrl : 'pages/know.html',
            controller  : 'knowController'
        })

        // route for the share page
        .when('/share', {
            templateUrl : 'pages/share.html',
            controller  : 'shareController'
        });

        // use the HTML5 History API
        // $locationProvider.html5Mode({
        //      enabled: true,
        //      requireBase: false
        //  });

    });

home.html页面加载得很好,URL显示如下:

http://localhost/postsafetyapp/#!/

如果我点击分享按钮,则网址会更改为:

http://localhost/postsafetyapp/#!/#share

但观点并没有改变。

如果我取消注释$ locationProvider代码,则网址变为:

http://localhost/postsafetyapp/(home.html页面加载没问题)

点击按钮,网址变为:

http://localhost/postsafetyapp/#share(视图不会改变)

我在本地服务器上运行WAMPSERVER。 任何帮助都会很棒!

1 个答案:

答案 0 :(得分:0)

您可以将控制器保留在app config

请将代码与plnkr链接进行比较

Click here了解plnkr中的工作代码。

如果您想查看网址如何路由,请使用以下链接

Seperate plnkr window (without code)

索引页面中的href

<ul class="nav navbar-nav navbar-right">
        <li><a href="#!/home"><i class="fa fa-home"></i> Home</a></li>
        <li><a href="#!/about"><i class="fa fa-shield"></i> About</a></li>
        <li><a href="#!/contact"><i class="fa fa-comment"></i> Contact</a></li>
      </ul>

在app config中定义的控制器:

// create the module and name it scotchApp
var scotchApp = angular.module('scotchApp', ['ngRoute']);

// configure our routes
scotchApp.config(function($routeProvider) {
  $routeProvider

  // route for the home page
    .when('/home', {
    templateUrl: 'pages/home.html',
    controller: 'mainController'
  })

  // route for the about page
  .when('/about', {
    templateUrl: 'pages/about.html',
    controller: 'aboutController'
  })

  // route for the contact page
  .when('/contact', {
    templateUrl: 'pages/contact.html',
    controller: 'contactController'
  })

  //otherwise redirect to home
  .otherwise({
    redirectTo: "/home"
  });
});

// create the controller and inject Angular's $scope
scotchApp.controller('mainController', function($scope) {
  // create a message to display in our view
  $scope.message = 'Everyone come and see how good I look!!!!';
});

scotchApp.controller('aboutController', function($scope) {
  $scope.message = 'Look! I am an about page.';
});

scotchApp.controller('contactController', function($scope) {
  $scope.message = 'Contact us!.';
});