【发布时间】:2015-06-16 02:39:18
【问题描述】:
我正在学习 AngularJS 路由,但似乎无法弄清楚为什么 $routeProvider 上的第二个 when() 不起作用。
index.html
<html lang="en" ng-app="myApp" class="no-js">
<head>
<meta charset="utf-8">
<title>My AngularJS App</title>
<link rel="stylesheet" href="bower_components/bootstrap/dist/css/bootstrap.css"/>
</head>
<body>
<div>
<ng-view></ng-view>
</div>
<!-- In production use:
<script src="//ajax.googleapis.com/ajax/libs/angularjs/x.x.x/angular.min.js"></script>
-->
<script src="bower_components/angular/angular.js"></script>
<script src="bower_components/angular-route/angular-route.js"></script>
<script src="app.js"></script>
</body>
</html>
app.js
'use strict';
// Declare app level module which depends on views, and components
var app = angular.module('myApp', [
'ngRoute'
]);
app.config(['$routeProvider', function ($routeProvider) {
$routeProvider.
when('/', {templateUrl: 'partials/li.html', controller: 'MainCtrl'}).
when('/new', {templateUrl: 'partials/edit.html'}).
otherwise({redirectTo: "/"})
}]);
app.controller('MainCtrl', ['$scope', 'persons', function ($scope, persons) {
$scope.persons = persons
}]);
app.factory('persons', [function () {
var persons = [
{fname: "Dave", lname: "Thomas"},
{fname: "John", lname: "Smith"},
{fname: "Jason", lname: "Doe"},
{fname: "Stupid", lname: "Guy"},
];
return persons;
}]);
li.html(由于某种原因,我无法在 WebStorm 中将其另存为 list.html)
<table class="table table-striped" style="width: auto" >
<thead>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<td><a href="/new"><span class="glyphicon glyphicon-plus"></span></a></td>
</tr>
</thead>
<tbody ng-repeat="person in persons">
<tr >
<td>{{person.fname}}</td>
<td>{{person.lname}}</td>
<td><a href="/edit"><span class="glyphicon glyphicon-edit"></span ></a></td>
</tr>
</tbody>
</table>
每当我单击加号图标时,都会引发 404 错误。即使在 IDE 中,app.js 中的第二个 when 的颜色也不同于第一个和 else() 的颜色。
编辑:还可以帮助我使用 $locationProvider,它在添加到 app.config() 时会引发此错误 - 错误:HTML5 模式下的 [$location:nobase] $location 需要存在标签!
【问题讨论】:
标签: javascript angularjs