无法实例化控制器Angular New Router

时间:2015-05-15 14:40:54

标签: angularjs angularjs-new-router

我正在使用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并且是一样的。我究竟做错了什么?一个非常相似的组件就像魅力一样,但事实并非如此。

1 个答案:

答案 0 :(得分: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

app.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}]}
  ]
});