Reputation: 971
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:
Upvotes: 17
Views: 3292
Reputation: 1554
The first thing that strike me, is that you're not using any identifier that points to the parent and child that you want to display.
How/where do you get this information? Your url would look a lot nicer (and maybe even solve the issue if it were written as
/parent/:parentId/
or
/parent/:parentId/child/:childId
I've mostly used routeProvider and routeParameters myself, and they provide this functionality without any hassle but as this exercise seems to be purely academic, i'd try to read through what the module is actually doing as well as checking whether there is a related issue among the github issues.
Further, this page from the documentation seem to shed some light on the issue.
If this is not at all helping you, would you consider helping us out by giving us some more information?
Good luck!
Upvotes: 1
Reputation: 181
You can also try using UI Router.
You can also have a look at this if you want to use nested routes.
Upvotes: 1
Reputation: 39
**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 .**
Upvotes: 1
Reputation: 39
var app = angular.module("app", ["ngRoute"]);
app.config(function($routeProvider) {
$routeProvider
.when("/", {
templateUrl : "parent.htm"
})
.when("/other", {
templateUrl : "Default.htm"
});
});
Upvotes: 1