I am using ngUpgrade and I dont know how to handle the routing between ng2 and ng1. If I am in ng2 component, how do I route to ng1 controller? and viceversa.
I am using ui-router for the ng1. For ng2 I am using the router on the angular team.
Thanks all!
First you need to have defined your routes as normal in both Angular 1.x and 2.x apps.
app.config(function($routeProvider) {
$routeProvider
// route for the home page
.when('/', {
templateUrl : 'pages/home.html',
controller : 'mainController'
})
// route for the about page
.when('/about', {
templateUrl : 'pages/about.html',
controller : 'aboutController'
})
// route for the contact page
.when('/contact', {
templateUrl : 'pages/contact.html',
controller : 'contactController'
});
});
And in the Angular 2 Module:
Module.config(($routeProvider) => {
$routeProvider
.when('/user/:id', {template : '<user-details></userdetails>'})
.when('/users', {template : '<user-list></userlist>'});
});
You can create a class called Ng1Ng2UrlHandlingStrategy and in there devide your routes between them:
class Ng1Ng2UrlHandlingStrategy implements UrlHandlingStrategy {
shouldProcessUrl(url) { return url.toString().startsWith("/home") || url.toString().startsWith("/aboute") || url.toString().startsWith("/contact"); }
extract(url) { return url; }
merge(url, whole) { return url; }
}
And in your main component:
providers: [
// Providing a custom url handling strategy to tell the Angular 2 router
// which routes it is responsible for.
{ provide: UrlHandlingStrategy, useClass: Ng1Ng2UrlHandlingStrategy }
]
Finally, update the root component to include an Angular 2 router outlet.
#Component({
selector: 'root-cmp',
template: `
<router-outlet></router-outlet>
<div class="ng-view"></div>
`,
})
export class RootCmp {}
Related
I have a project that I want to link views together from different controllers so my controllers are
Dashboard and Board
and my views are
Dashboard
Index (Main view with ng-app and ng-view)
Test page
Board
BoardIndex (A view I want loaded into /Dashboard/Index's ng-view)
In my main.js file I have:
angular.module('App', ['ngRoute', 'ngResource']);
angular.module('App').config(function ($routeProvider) {
$routeProvider
.when('/', {
templateUrl: 'Dash',
title: 'Dashboard'
})
.when('/PageOne', {
templateUrl: 'PageOne',
title: 'Page One'
})
.when('/Board', {
templateUrl: 'Board',
title: 'Board'
})
.when('/Messenger', {
templateUrl: 'Messenger/Messenger',
title: 'Messenger'
})
.otherwise({redirectTo: '/'})
});
and my link to messenger looks like this: /Dashboard/#/Messenger
The link above will hit the Messenger controller ActionResult that routes to the BoardIndex view, but it doesn't actually show like I expect it to which would be the url being: URL/Dashboard/#/Messenger with the content of BoardIndex showing. Instead it goes to URL/Messenger with a blank page.
I need some help wiring this up.
Thanks!
here I have defined a name of the controller as dashboard and board . please verify the controller's name from your controllers. this might help you. let me know if you are expecting something else
angular.module('app')
.config(['$stateProvider','$urlRouterProvider',function($stateProvider,$urlRouterProvider){
$stateProvider
.state('dash',{
url : '/',
templateUrl : 'dash',
controller : 'dashboard'
})
.state('pageOne',{
url : '/pageOne',
templateUrl : 'pageOne',
controller : 'dashboard'
})
.state('board',{
url : '/board',
templateUrl : 'board',
controller : 'board'
})
.state('messenger',{
url : '/Messenger',
templateUrl : 'Messenger/Messenger',
controller : 'dashboard'
})
$urlRouterProvider.otherwise('dash');
}])
I am currently working on an Angular app and facing some issues with URLs.
Problem:
My application has the main page which has multiple sections and all sections fetch data from backend. There is an animated scroller on each section, e.g. If I am at section1 and select Section3, it scrolls me to Section3 in a nice animated way. I have few pages which also are fetched from backend but totally changes the view. This is the hierarchy of views:
Page1:
Section1, Section2, Section3, Section4, Section5 (Will be visible in whole application aka HEADER)
Section5:
Link1, Link2 (Each link is totally a separate view)
If I am on Link1 and click any of the Section, it appends # to the URL relatively and doesn't redirect me to the actual section.
Question(s):
I do not want to use # anywhere at all, how can I get rid of it?
I do not want to remove animation from my home page.
If I am on some other view and click on any section (HEADER), it should take me to that particular position, how can I achieve this?
This is my routes.js file:
var webApp = angular.module('webApp', ['ngRoute','ngSanitize']);
webApp.config(function($routeProvider, $locationProvider) {
if(window.history && window.history.pushState) {
$locationProvider.html5Mode({
enabled: true,
requireBase: false
});
}
$routeProvider
.when('/', {
templateUrl : 'application/header.jsp',
controller : 'homeController'
})
//section2
.when('/section2', {
templateUrl : 'application/header.jsp',
controller : 'homeController'
})
.when('/section3', {
templateUrl : 'application/header.jsp',
controller : 'homeController'
})
.when('/section4', {
templateUrl : 'application/header.jsp',
controller : 'homeController'
})
.when('/section5', {
templateUrl : 'application/header.jsp',
controller : 'homeController'
})
.when('/section7', {
templateUrl : 'application/header.jsp',
controller : 'homeController'
})
.when('/section8', {
templateUrl : 'application/header.jsp',
controller : 'homeController'
})
.when('/Link1', {
templateUrl : 'application/link1.jsp',
controller : 'link1Controller'
})
.when('/Link2', {
templateUrl : 'application/link2.jsp',
controller : 'link2Controller'
})
.when('/Link3', {
templateUrl : 'application/link3.jsp',
controller : 'link3Controller'
});
});
Any help will be greatly appreciated or if anything is missing or required, please do let me know.
Using my local host, i was worked my angular app. i moved this app to my dev environment, where i am working now.
But after i move to dev my router not working. i guess i need to do some configuration to work that. But i tried up to changing my routes but nothing works.
my new dev app url : http://azvsp14devapp01:123/_layouts/15/cpmd/public/ - in the public folder my index.html nested.
here is my app.js :
(function () {
"user strict";
angular.module("tcpApp", ["ngRoute","ngResource", "ngAnimate"])
.run(['$route', function ($route) {
$route.reload();
}])
.config(function ($routeProvider, $locationProvider) {
// $locationProvider.html5Mode(true);
$routeProvider
.when ("123/_layouts/15/cpmd/public/", { //trying for equalient of `/`
templateUrl : "views/login/login.html",
controller : "loginController",
className : "body-login"
});
$routeProvider
.when ("/lock", {
templateUrl : "views/lock/lockScreen.html",
controller : "lockScreenController",
className : "body-lockScreen"
});
$routeProvider
.when ("/home", {
templateUrl : "views/home/home.html",
controller : "homeController",
className : "body-home"
});
$routeProvider
.when ("/projectSummary/:id", {
templateUrl : "views/projectSummary/projectSummary.html",
controller : "projectSummaryController",
className : "body-projectSummary"
});
$routeProvider
.when ("/projectDetails", {
templateUrl : "views/projectDetails/projectDetails.html",
controller : "projectDetailsController",
className : "body-projectDetails"
});
$routeProvider
.otherwise ({
redirectTo:'123/_layouts/15/cpmd/public/'
});
})
})();
Change
"123/_layouts/15/cpmd/public/"
to
"/"
In both when and otherwise.
This because router place with index.html treats as root.
How can I perform a matchmedia for page routing below. That is I would like load a different templateURL for home if it is a tablet?
app.config(['$routeProvider',function($routeProvider) {
//configure the routes
$routeProvider
.when('/',{
// route for the home page
templateUrl:'partials/login/login.html',
controller:'loginCtrl'
})
.when('/home',{
// route for the home page
templateUrl:'partials/home/home.html',
controller:'mainCtrl'
})
.otherwise({
//when all else fails
templateUrl:'partials/login/login.html',
controller:'loginCtrl'
});
}]);
You could simply achieve this by adding function while generation templateUrl,
before returning templateUrl to check navigator string and browser.
Code
$routeProvider
.when('/', {
// route for the home page
templateUrl: function() {
if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) {
return 'partials/mobile/login/login.html'; //mobile template
} else {
return 'partials/login/login.html';
}
},
controller: 'loginCtrl'
})
How can I specify multiple providers to AngularJS?
Right now I'm doing it in 2 places, but this doesn't seem to work:
var myApp = angular.module("myApp", ['ngRoute']);
// configure our routes
myApp.config(function($routeProvider) {
$routeProvider
// route for the home page
.when('/', {
templateUrl : 'pages/home.html',
controller : 'mainController'
})
// route for the about page
.when('/about', {
templateUrl : 'pages/about.html',
controller : 'aboutController'
})
// route for the contact page
.when('/contact', {
templateUrl : 'pages/contact.html',
controller : 'contactController'
});
});
myApp.config(function($interpolateProvider){
$interpolateProvider.startSymbol('((').endSymbol('))');
}
);
Use a single function reference:
myApp.config(foo)
which injects each provider:
function foo($routeProvider, $interpolateProvider)
{
/* interpolateProvider configuration */
$interpolateProvider.startSymbol('((').endSymbol('))');
/* routeProvider configuration */
$routeProvider
.when('/', {
templateUrl : 'pages/home.html',
controller : 'mainController'
})
.when('/about', {
templateUrl : 'pages/about.html',
controller : 'aboutController'
})
.when('/contact', {
templateUrl : 'pages/contact.html',
controller : 'contactController'
});
}
/* module creation */
var myApp = angular.module("myApp",[]);
/* provider binding */
myApp.config(foo);
/* Manual bootstrapping */
angular.bootstrap(document, ['myApp']);
/* $inject Property Annotation */
foo.$inject['$routeProvider', '$interpolateProvider'];
Providers are essentially objects that are used to create and configure instances of AngularJS artefacts. Hence, in order to register a lazy controller, you would use the $controllerProvider. Similarly, To register a directive, you would use the $compileProvider, to register filters you would use the $filterProvider, and to register other services, you would use the $provide service.
References
AngularJS: Configuration Blocks
AngularJS: Inject Property Annotation