Could not instantiate controller Angular New Router

997 Views Asked by At

I'm using the new Angular router for 1.4 and it throws me an error when doing a simple controller for a component:

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;
}

I've tried without injecting $rootScope and is the same. What am I doing wrong? A very similar component works like charm, but this doesn't.

1

There are 1 best solutions below

2
On

here is latest example of new router in use with angular 1.5, for 1.4 you won't be able to use component() I think unless you add it yourself, it is just a wrapper around directive though.

You should use angular_1_router.js built from angular2 project instead of installing package of angular-new-router (until it is updated) in below example they use angular 1.5 and fixed it so you can use components() and child routes etc.

check link for full working example: 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}]}
  ]
});