Component Router (AngularJS 1.5) useAsDefault config - angularjs

I am new to Component Router. I am using it in angular 1.5 and I would like to define a default component in the case a user try to access a route not defined in the config :
I have an app component with this config :
$routeConfig: [
{ path: '/...', name: 'Layout', component: 'layout', useAsDefault: true }
]
and a layout component with this config
$routeConfig: [
{path: '/', name: 'Home', component: 'home', useAsDefault: true},
{path: '/user', name: 'User', component: 'user' }
]
I can access to the urls localhost:8080/#/ and localhost:8080/#/user but
when I try to access localhost:8080/#/test it doesn't redirect me to the home component although the useAsDefault is set to true (I obtain a blank page...).
Has anyone faced this problem ?

Related

Can I use nested components linked with same app module? Is it good practise? Using component Router

Here is component structure of my application. Any advice will be valuable. Root component is app.
Which has two components as child : login & home.
home has two child components : dummy-component & dummy-component1.
Is it good practice? Will I face any issue if I switch component router to ui-router. I am also planning to switch from angular 1.6.0 to 2.0
App.component('app', {
templateUrl: 'core/app',
$routeConfig: [{
path: '/core/teamnest/login-component/',
name: 'LoginComponent',
component: 'loginComponent',
useAsDefault: true
},
{
path: '/core/teamnest/home-component/...',
name: 'HomeComponent',
component: 'homeComponent'
},
],
controller: appController
});
App.component('homeComponent', {
templateUrl: 'core/dashboard',
$routeConfig: [{
path: '/dummy-component',
name: 'DummyComponent',
component: 'dummyComponent',
useAsDefault: true
},
{
path: '/dummy-component1',
name: 'DummyComponent1',
component: 'dummyComponent1'
}
],
bindings: {
$router: '<'
},
controller: homeController
});
App.component('dummyComponent', {
templateUrl: 'core/dummy',
controller: dummyController
});
App.component('dummyComponent1', {
templateUrl: 'core/diff'
});

Angular Router nested named outlets

I'm using Angular (v2.4.5). Is it possible to have nested named outlets?
Here is my situation:
//app.component.html
<router-outlet name="test"></router-outlet>
<router-outlet></router-outlet>
//dashboard.component.html
<router-outlet name="test"></router-outlet>
<router-outlet></router-outlet>
Routes:
const routes: Routes = [
{
path: '',
redirectTo: '/dashboard',
pathMatch: 'full'
},
{
path: 'login',
component: LoginComponent
},
{
path: 'dashboard',
component: DashboardComponent,
canActivate: [AuthGuard],
children: [
{
path: '',
redirectTo: 'inbox',
pathMatch: 'full'
},
{
path: 'inbox',
component: InboxContentComponent
},
{
path: 'test',
component: TestComponent,
outlet: 'test'
}
],
{
path: 'test',
component: TestComponent,
outlet: 'test'
}
}
If I go to this url: http://localhost/login(test:test) the TestComponent is loaded in the app.component.html outlet.
If I go to this url: http://localhost/dashboard/inbox(test:test) the TestComponent is loaded in the app.component.html outlet and not in dashboard.component.html
I want to be able to target test outlet from dashboard. Is it possible?

Angular 2 routing issue

Can somebody help me with angular 2 routing.
I have 2 pages.. home and search results page..
Header for home and search results page are different..
here is my code, I am able to display the home page with header but when I go to search results page, header is not getting replaced with new one..
home.routes
export const HomeRoutes: Routes = [
{ path: '', component: HomeComponent},
{ path: '', component: HomeHeadbarComponent, outlet: 'route1' }
];
search.routes
export const SearchRoutes: Routes = [
{ path: 'search', component: SearchPanelComponent},
{ path: 'search', component: HeadbarComponent, outlet: 'route1' }
];
App.html
<router-outlet name="route1"></router-outlet>
<router-outlet></router-outlet>
Appreciate your help
Your home.routes should be below code this will be redirect to you HomeComponent and if you want to redirect it on SearchPanelComponent both you can use in below code
import { Routes, RouterModule } from '#angular/router';
import { HomeComponent} from './Home';
export const routes: Routes = [
{ path: '', redirectTo: 'Home', pathMatch: 'full' },
{ path: 'Home', component: HomeComponent},
{ path: 'search', component: SearchPanelComponent},
];
export const appRoutingProviders: any[] = [
];
export const routing = RouterModule.forRoot(routes);
I assume, that happens because you have empty path path: '' for the first header, so it could overlap the new one. Could you try with the different path, and check it out, should help.
got it working with the code below..
{
path: 'search',
children: [
{
path: '',
component: SearchPanelComponent
},
{
path: '',
component: HeadbarComponent,
outlet: 'route1'
}]
}

RC5 Angular2 route default component

I have existing PHP website, which I start adding angular2 component to.
I have added router script, to help load different component by its url.
When I navigate away from the component page, I get following error Error: Uncaught (in promise): Error: Cannot match any routes: 'dashboard'
I understand, that I need to create another component to make this error disappear. But I don't want to create another component, because there are so many pages, I would end up creating component for each page.
So I wanted to ask, how to make 1 default component, which will pickup if no component was available OR how can I just make this error disappear, so it does not trigger if it cant found any router or component for that path.
import { ModuleWithProviders } from '#angular/core';
import { Routes, RouterModule } from '#angular/router';
import {TestComponent} from "./test/test.component";
const appRoutes: Routes = [
{ path: 'search', component: TestComponent }
];
export const appRoutingProviders: any[] = [
];
export const routing: ModuleWithProviders = RouterModule.forRoot(appRoutes);
you may add a wildcard route which will redirect to a default component if route does not match.
{ path: 'default', component: DefaultComponent },
{ path: '**', redirectTo: '/default' }
So if the route does not match your default component will be loaded in the router outlet.
Hope this helps!!
I use this as default route:
{ path: '', component: Home, text: 'Home' }
and this is my full routes:
export const routes: TextRoute[] = [
{ path: '', component: Home, text: 'Home' },
{ path: 'accordion', component: AccordionDemo, text: 'Accordion' },
{ path: 'alert', component: AlertDemo, text: 'Alert' },
{ path: 'buttons', component: ButtonsDemo, text: 'Buttons' },
{ path: 'carousel', component: CarouselDemo, text: 'Carousel' },
{ path: 'collapse', component: CollapseDemo, text: 'Collapse' },
{ path: 'dropdown', component: DropdownDemo, text: 'Dropdown' },
{ path: 'pagination', component: PaginationDemo, text: 'Pagination' },
{ path: 'popover', component: PopoverDemo, text: 'Popover' },
{ path: 'progressbar', component: ProgressbarDemo, text: 'Progressbar' },
{ path: 'rating', component: RatingDemo, text: 'Rating' }
];
export const AppRoutes = RouterModule.forRoot(routes, { useHash: true });
Check out my learning angular2 project at github, stars are welcome.
Im too new to comment, my question is if you are using express backend? app.use() might be able to save you some trouble. instead of declaring the routes try in app.js
var routes = require('routes');
app.use('/name-of-url', routes )
then in routes
router.get('name-of-url', function(res,req,next){
res.sendFile(path to php file to serve)
})
You may also need to change your express templating engine to php, not entirely sure.

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?
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}]}
]
});

Resources