kennyg
kennyg

Reputation: 1059

How to use Angular New Router without a Controller

I thought the new direction that Angular was going in was Controller free views. How come the New Router (seemingly) asks for a Controller? Is it possible to route without one?

Upvotes: 1

Views: 96

Answers (1)

Andrew Arnautov
Andrew Arnautov

Reputation: 343

Yes it is possible. In 1.5 You can use component() or 1.3+ directive(). Here is the latest example working with angular 1.5, components() and child routes: 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}]}
  ]
});

Upvotes: 1

Related Questions