I don't know how to do wildcard path routing in an app that's half-upgraded from AngularJS to ng2.
In general, you mix both kinds of routing like this:
The first step to have a dual router setup is to add an Angular root
component containing one outlet for each router. AngularJS will use
ng-view, and Angular will use router-outlet. When one is using it's
router, the other outlet will be empty.
import { Component } from '#angular/core';
#Component({
selector: 'my-app',
template: `
<router-outlet></router-outlet>
<div ng-view></div>
`,
})
export class AppComponent { }
https://angular.io/docs/ts/latest/guide/upgrade.html#!#dividing-routes-between-angular-and-angularjs
In Angular 2 you can specify a wildcard path like this:
{ path: '**', component: PageNotFoundComponent }
In AngularJS you can have a wildcard path like this:
$routeProvider
.otherwise({
template: '<page-not-found></page-not-found>',
});
When I put everything together and a path isn't handled, how do avoid both routers from emitting <page-not-found> ?
I can confirm that <div ng-view></div> does work in the angular 2 AppComponent component if everything else is set up right. (Add AppComponent to AppModule's bootstrap array).
Use HybridUrlHandlingStrategy to prevent Angular from throwing an error when an ng1 route is requested.
Add dummy routes with empty ("") templates to prevent ng1 from rendering 404 page when an ng2 page is requested:
$routeProvider
.when('/some/ng1/path', {template: '<something></something>'})
.when('/some/ng2/path', {template: ''}) // ng2 route
.otherwise({template: '<page-not-found></page-not-found>'});
Related
I have an Angular 14 application that has the build files for a React application being served in a folder alongside it, as such.
dist
|_apps
|_react-app-build
|
|
|_main.js, runtime.js, etc
my routing-module is as follows
{path: '', component: HomepageComponent},
{path: 'projects', component: ProjectsPageComponent},
{path: 'contact', component: ContactPageComponent},
{path: '**', redirectTo: "/"},
I need to be able to access the react app, but its routes keep being picked up by the Angular router and redirected to the root. Is there any way to force the Angular router to completely ignore the routes for the React application (/apps/react-app-build/*)?
So far I have tried setting the base href to something other than the root <base href="/site">
as well as adding
#NgModule({
imports: [RouterModule.forRoot(routes,
{
---
useHash: true
---
})],
exports: [RouterModule]
})
to the routing module, all to no avail.
Read more on Webpack Module Federation. It allows you to share separate JavaScript applications using lazy loaded routes. Does not matter whether the application is react or angular
I am running UI Router with angular 4.x. Below code is not rendering anything and I did not get any error message too. But when I changed to $default as a view name, then I am getting the page.Please suggest me.
<app-root>
<ui-view name='main'></ui-view>
</app-root>
Below is the angular State Definition,
export const appState = {
name: 'app',
views : {
main : {
component: AppComponent
}
}
};
When Angular application bootstrap's, it wipes out the inner content of app-root(root) component. What ever you put inside root component will appear until Angular application bootstrap. Generally this place has been used to add Splash screen, loader to show initial loading.
To see your ui-view to replace via ui-router configuration, you should add ui-view inside app-root component HTML.
#Component({
selector: 'app-root',
template: `<ui-view name='main'></ui-view>`
})
export AppRootComponent {
}
This question might sound very generic to some of you but as a newbie i am having trouble in this. Its evident to use ng-view within the home page in order to display other html files within the page but how should i redirect to a new page present in the web app. I mean how to route to completely different web page in a multipage web application.
Import AngularJs-Route File
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-route.js"></script>
Then you must add the ngRoute as a dependency in the application module:
var app = angular.module("myApp", ["ngRoute"]);
Use the $routeProvider to configure different routes in your application:
app.config(function($routeProvider) {
$routeProvider
.when("/", {
templateUrl : "main.htm"
})
.when("/red", {
templateUrl : "red.htm"
})
.when("/green", {
templateUrl : "green.htm"
})
.when("/blue", {
templateUrl : "blue.htm"
});
});
STructure Your HTML
<body ng-app="myApp">
<p>Home</p>
Red
Green
Blue
<div ng-view></div>
</body>
For nested views you can use https://github.com/angular-ui/ui-router
Follow https://github.com/angular-ui/ui-router/wiki/Nested-States-&-Nested-Views for reference
Try searching angular ui-roter how its works and its mechanism . Since angular is a single page application your app needs to be on one base template then expand from their. From base template route in different page but if you want to route to different application use normal hyper link or ui-serf . Go though u-router basic. Also look into ui-serf .
Is there any way to load JS selectively depending on module?
I want to have Bootstrap.css included in templates that are in frontend and Angular Material for temaplates that are in backend of my app.
Currently if I import bootstrap.css in one component it's loaded globally which I don't want. This also means that backend.css and ngMaterial will also be loaded in frontend components...
So the way to conditionally load CSS I came up with is not very elegant but works.
I'm using Angular UI Router and when it comes to resolve route I append/remove link tag with CSS sheet using angular.element(). In my case it's abstract routes for backend and frontend, therefore it resolves (and appends/removes) link tag only once when browsing children routes.
// Frontend - add Bootstrap
$stateProvider
.state('front', {
url: "",
abstract: true,
templateUrl,
resolve: {
importBootstrapCss() {
let head = angular.element(document.querySelector('head'));
head.append("<link rel='stylesheet' type='text/css' href='//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css' id='bootstrap-css'>");
}
}
});
// Backend - remove Bootstrap
$stateProvider
.state('backend', {
url: "/backend",
abstract: true,
templateUrl,
resolve: {
deleteBootstrapCss() {
let link = angular.element(document.getElementById('bootstrap-css'));
link.remove();
}
}
});
My web application has multiple filters that should be represented in the current Url so that users can simply copy/bookmark the current page to get it back later.
In angular1 I used $location.search(name, value) to simply set seach params on change. Now I want to get something similar in angular2.
Or is it wrong?
I think you must use the router inside Angular2. code example:
import {Component} from 'angular2/core';
import {RouteConfig, ROUTER_DIRECTIVES} from 'angular2/router';
import {ProfileComponent} from './profile.component';
#Component({
selector: 'my-app',
template: `
<router-outlet></router-outlet>
`,
directives: [ROUTER_DIRECTIVES]
})
#RouteConfig([
{path: '/profile/:id', name: 'Profile', component: ProfileComponent}
])
export class AppComponent {
}
The :id is a route parameter and you can do an URL like that:
/profile/2 and 2 is the value of the id parameter.
You could find more detail in the Angular2 doc : router doc