5

我正在为 1.4 使用新的 Angular 路由器,在为组件执行简单控制器时它会引发错误:

angular.module('app.campaigns',['security', 'ngNewRouter'])
  .controller('CampaignsController', ['authService', '$rootScope', CampaignsController]);   


function CampaignsController (authService, $rootScope) {
    console.log ('this is campaigns controller');
    this.currentUser = $rootScope.authService.currentUser;
    this.authService = $rootScope.authService;
    this.logout = authService.logout;
}

我试过不注入 $rootScope 并且是一样的。我究竟做错了什么?一个非常相似的组件就像魅力一样工作,但事实并非如此。

4

1 回答 1

0

这是与 angular 1.5 一起使用的新路由器的最新示例,对于 1.4,您将无法使用 component() 我认为除非您自己添加它,否则它只是指令的包装器。

您应该使用从 angular2 项目构建的 angular_1_router.js,而不是在下面的示例中安装 angular-new-router 包(直到它更新),他们使用 angular 1.5 并修复它,以便您可以使用 components() 和子路由等。

检查完整工作示例的链接: http ://plnkr.co/edit/N3YP3dKMuljpZ6mWsVBT?p=preview

应用程序.js

angular.module('app', ['ngComponentRouter', 'dialog', 'heroes', 'crisis-center'])

.config(function($locationProvider) {
  $locationProvider.html5Mode(true);
})

.run(function($router) {
  $router.config([
    { path: '/...', name: 'App', component: 'app', useAsDefault: true }
  ]);
  $router.navigate(['App']);
})

.component('app', {
  template:
    '<nav>\n' +
    '  <a ng-link="[\'CrisisCenter\']">Crisis Center</a>\n' +
    '  <a ng-link="[\'Heroes\']">Heroes</a>\n' +
    '</nav>\n' +
    '<ng-outlet></ng-outlet>\n',
  $routeConfig: [
    {path: '/crisis-center/...', name: 'CrisisCenter', component: 'crisisCenter', useAsDefault: true},
    {path: '/heroes/...', name: 'Heroes', component: 'heroes'},
    {path: '/disaster', name: 'Asteroid', redirectTo: ['CrisisCenter', 'CrisisDetail', {id:3}]}
  ]
});
于 2016-02-06T11:33:33.647 回答