我正在尝试遵循这个教程:
https://angular.github.io/router/getting-started为什么AppController。$ routeConfig定义?
angular.module('app', ['ngNewRouter', 'app.home'])
.controller('AppController', ['$router', AppController]);
AppController.$routeConfig([
{path: '/', component: 'home' }
]);
function AppController ($router) {}
我的html:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<base href="/">
<title>My app</title>
</head>
<body ng-app="myApp" ng-controller="AppController as app">
<div ng-viewport></div>
<script src="node_modules/angular/angular.js"></script>
<script src="node_modules/angular-new-router/dist/router.es5.js"></script>
<script src="./components/home/home.js"></script>
<script src="app.js"></script>
</body>
</html>
试试这个代替,为我工作:
AppController.$routeConfig = [
{ path: '/', component: 'home' }
]
我想这可能是教程中的错误。$ routecconfig是一个属性,而不是一个函数。
我发现这个手册适合我使用angular 1.5,但作者使用的是angular 1.3http://www.sitepoint.com/introduction-futuristic-new-router-angularjs/
下面是为你工作的代码:
angular.module('app', ['ngNewRouter', 'app.home'])
.controller('AppController', ['$router', function($router){
$router.config([
{ path:'/', redirectTo:'/home' },
{ path:'/home', component:'home' },
{ path:'/example', component:'example' }
]);
this.name='visitor';
}]);
,在HTML中将这两行改为:
<body ng-controller="AppController">
<div ng-viewport></div>