Angular Routes不起作用

时间:2017-03-20 15:23:25

标签: javascript angularjs angularjs-directive angular-ui-router angular-routing

app.js 我正在尝试在这里设置路线,但它无法正常工作,我不知道为什么它不起作用,请仔细研究这个问题。当我点击第1页时,网址应该是/员工,但它的形状有点奇怪 http://localhost:3000/#!#employees

var myapp = angular.module('myapp',['ngRoute'])
myapp.config(function($routeProvider){
console.log('in the routes')
$routeProvider
    .when('/employees' ,{
        templateUrl: 'templates/index.html',
        controller: 'empController'
    })
    .when('/employees/create' ,{
        templateUrl: 'templates/create.html',
        controller: 'empController'
    })
    .when('/employees/:id/show' ,{
        templateUrl: 'templates/show.html',
        controller: 'empController'
    })
    .when('/employees/:id/edit' ,{
        templateUrl: 'templates/edit.html',
        controller: 'empController'
    })
})

的index.html

<body ng-app='myapp' ng-controller='empController'>
<nav class="navbar navbar-default">
  <div class="container-fluid">
    <div class="navbar-header">
      <a class="navbar-brand" href="">WebSiteName</a>
    </div>
    <ul class="nav navbar-nav">
      <li class="active"><a href="">Home</a></li>
      <li><a href="#/employees">Page 1</a></li>
      <li><a href="#/employees/create">Page 2</a></li>
      <li><a href="#/employees/:id/edit">Page 3</a></li>
      <li><a href="#/employees/:id/show">Page 3</a></li>
    </ul>
  </div>
</nav>
<hr>

<div ng-view></div>

2 个答案:

答案 0 :(得分:1)

试试这种方式,

var myapp = angular.module('myapp',['ngRoute'])
myapp.config(function($routeProvider, $locationProvider){
$locationProvider.hashPrefix('');
console.log('in the routes')
$routeProvider
    .when('/employees' ,{
        templateUrl: 'templates/index.html',
        controller: 'empController'
    })
    .when('/employees/create' ,{
        templateUrl: 'templates/create.html',
        controller: 'empController'
    })
    .when('/employees/:id/show' ,{
        templateUrl: 'templates/show.html',
        controller: 'empController'
    })
    .when('/employees/:id/edit' ,{
        templateUrl: 'templates/edit.html',
        controller: 'empController'
    })
})

答案 1 :(得分:0)

您可以修改您的应用。 js是这样的,以便处理它。

.config(function ($routeProvider,$locationProvider) {
    $locationProvider.hashPrefix('');
    $routeProvider