Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular new router - Nested components and routing

I'm playing around with the new angular router and wanted to try out a use case where I have a component and a nested component.

Below there's the JavaScript code I wrote to define the two components and the routes:

angular.module('app', ['ngNewRouter'])
  .controller('AppController', function ($router) {

  $router.config([
    {
      path: '/parent',
      component: 'parent'
    }
  ]);

})
.controller('ParentController', function ($router) {

  this.name = 'Parent component';
  $router.config([
    {
      path: '/child',
      component: 'child'
    }
  ]);

})
.controller('ChildController', function () {

  this.name = 'Child component';

})
.config(function ($componentLoaderProvider) {

  $componentLoaderProvider.setTemplateMapping(function (compName) {
    return compName + '.html';
  });  

});

And the HTML part:

<!-- index.html -->
<body ng-controller="AppController as app">
    <a ng-link="parent">Go to parent</a>
    <div ng-viewport></div>
</body>

<!-- parent.html -->
{{ parent.name }}<br/>
<a ng-link="child">Show child</a>
<div ng-viewport></div>

<!-- child.html -->
{{ child.name }}

Here's a Plunker containing the code above: http://plnkr.co/edit/yWCFgXQI491EYvIldjyR

Based on this code I have the following questions/issues:

  1. If I go to the lowest level (#/parent/child) and then hit refresh, the parent and child components are not shown anymore. The route is not restored even though the URL still is the same. Do I need to renavigate or do something to restore the page's state? This is a very basic feature to be able to bookmark URLs.
  2. If I go to the lowest level (#/parent/child) and then click on the Go to parent link, the URL is correctly set to #/parent but the child component is still visible.
like image 472
Alain Avatar asked Dec 19 '25 04:12

Alain


1 Answers

**Using new way we can do perform routing this .**


(function () {
  'use strict';

  angular
    .module('app', ['parent', 'home', 'ngNewRouter'])
    .controller('AppController', function ($router) {
      this.header = 'TITLE';
      $router.config([
        {
          path: '/', component: 'home'
        },
        {
          path: '/parent', component: 'parent'
        }
      ]);
    });
})();
**I hope this will work .**
like image 56
laxmikant yadav Avatar answered Dec 21 '25 19:12

laxmikant yadav



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!