Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 1.5 and new Component Router

I'm using angular 1.5 beta 2 and the new router from Angular 2 alpha 45.
I couldn't find examples of usage for the latest router with Angular 1.
I can find examples of the usage of the router for Angular 2, using @RouteConfig.

Can someone explain how I would configure an angular 1 controller? And, if possible, a full working example?

like image 892
pauloya Avatar asked Nov 11 '15 14:11

pauloya


People also ask

Can we have multiple routes in Angular?

Angular Router supports multiple outlets in the same application. A component has one associated primary route and can have auxiliary routes. Auxiliary routes enable developers to navigate multiple routes at the same time.

Is router mandatory for all applications in Angular?

simple answer is, no. It is not mandatory to have routes defined on every component you create.

What modules do you need to add for using Angular router?

Add the AppRoutingModule link The router is dedicated to routing and imported by the root AppModule . By convention, the module class name is AppRoutingModule and it belongs in the app-routing.module.ts in the src/app directory. Run ng generate to create the application routing module.


2 Answers

Update They have stopped working on this version of the Router and started a version 3 with different APIs. As of June 20, 2016 there was no recommended way for using the router v3 with Angular 1. I'm not sure if this has changed since. This question and answer below relates to Router v2 (aka ComponentRouter).


Obsolete API
A few sites indicate that a component in Angular 1 (for the purpose of the new router) is a controller registered as [name]Controller and a template picked up from component/[name]/[name].html. This is now obsolete.

New API
This discussion contains recent information, explaining how to get the latest Angular 1 new router version.

The component used in the configuration is mapped to a directive registered with the component name. See this sample.

angular.module('app.home', [])
.directive('home', function() {
  return {
    template: 'Hello {{ home.text }}',
    controller: function HomeController() {
      this.text = 'World';
    },
    controllerAs: 'home'
  }
});

With Angular 1.5 there is a new syntax for registering components, see here. I've used it with this syntax:

angular.module('app.home', [])
.component('home', {
  restrict: "EA",
  template: 'Hello {{ home.text }}',
  controller: function HomeController() {
    this.text = 'World';
  }
  // to configure a child route
  //,$routeConfig: [
  //  { aux: "/son", component: "son", as: "Left" },
  //  { aux: "/daughter", component: "daughter", as: "Left" }]
});
like image 182
pauloya Avatar answered Oct 01 '22 02:10

pauloya


Although it is pretty new at this point you can also use a root component with the new angular router. This component could take additional components as children.

I followed Pete Bacon Darwin's example to get a version going. https://github.com/petebacondarwin/ng1-component-router-demo

Notice in his version The main component has $router.config passed in the run block and identifies children with 3 dots.

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

I am using angular 1.5.0 to follow his github. I ran into issues playing with release some of the release candidates.

like image 28
Winnemucca Avatar answered Oct 01 '22 03:10

Winnemucca