Angular ui router dynamic routes - angularjs

I am using the following library
#version v0.2.11
#link http://angular-ui.github.com/
What i am looking at is for dynamic routing
for ex: for route localhost:8080/#/about it goes to function, let say routedFunction which defines the template html <div>about me .. </div>
and if someone goes to localhost:8080/#/abc ; routedFunction will get a routing url as parameter abc and it will know display that .. may be <div>abc xyz abc</div>
I am unable to find a solution for this.

You need to use a router.
Angular documentation for $route can be found at # https://docs.angularjs.org/api/ngRoute/service/$route
Please find and have a look at following example in documentation. This example shows how changing the URL hash causes the $route to match a route against the URL, and the ngView pulls in the partial.
<div ng-controller="MainController">
Choose:
Moby |
Moby: Ch1 |
Gatsby |
Gatsby: Ch4 |
Scarlet Letter<br/>
<div ng-view></div>
<hr />
<pre>$location.path() = {{$location.path()}}</pre>
<pre>$route.current.templateUrl = {{$route.current.templateUrl}}</pre>
<pre>$route.current.params = {{$route.current.params}}</pre>
<pre>$route.current.scope.name = {{$route.current.scope.name}}</pre>
<pre>$routeParams = {{$routeParams}}</pre>
</div>
Here is another example for defining different routes.
scotchApp.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'
});
});
To learn and understand router/routing the easy way # https://scotch.io/tutorials/single-page-apps-with-angularjs-routing-and-templating

Related

'/:routeparams' is overriding other route in angularjs

app.config(function($routeProvider) {
$routeProvider
.when('/', {
templateUrl : 'junk.html',
})
. .when('/:pageuniqueid', {
templateUrl : 'page.html',
})
// route for the about page
.when('/first', {
templateUrl : 'first.html',
})
// route for the contact page
.when('/second', {
templateUrl : 'second.html',
});
});
If i type 'example.com/first' in the URL then instead of getting first.html i am getting page.html.
I am implementing the page that user can access directly with their dynamic pageid after base URL.
I want to get page.html only if it is not matched with the other routes. Is there a way to achieve this?
The order of the route definitions matters.
If you define the '/first' route before the '/:pageuniqueid', it should work.
The angular router stops evaluating the routes after the first match.
So in order to get the page.html as a fallback, you should put it as last entry in the list.

Angular Routing with ngRoute doesn't work for some routes

In my single page application I have used ngRoute for angular routing. But I am facing some problems for angular routing.
configuration :
app.config(function($routeProvider,$locationProvider){
$locationProvider.html5Mode(true);
$locationProvider.hashPrefix('!');
$routeProvider
.when('/product',{
templateUrl : 'views/product/template/product.html',
controller : 'productCtrl'
})
.when('/product/add',{
templateUrl : 'views/product/template/add-product.html',
controller : 'productCtrl'
})
});
When my routing path is /product then everything is ok. But when my routing path is /product/add then it seems an error. But if "/product/add"
replace with just "/add" then that route is also ok. So I cant recognize whats the main problem on routing "/product/add".
Error :
http://localhost:3000/product/views/product/template/add-product.html 404 (Not Found)
here I have seen a problem. other route seems like :
http://localhost:3000/views/product/template/product.html
But error route has /product after server link.
Need some clarification & solution for this problem.
Thanks in advance
configure the route properly with error pages. You can follow this fiddle example.
HTML
<div ng-controller="MainCtrl">
<ul>
<li>product</li>
<li>add</li>
<li>Other</li>
</ul>
<ng-view></ng-view>
</div>
Angular
var app = angular.module( "myApp", [] );
app.config( function ( $routeProvider,$locationProvider ) {
$locationProvider.html5Mode(true);
$locationProvider.hashPrefix('!');
$routeProvider
.when( '/product', { template: 'I am product root' } )
.when( '/product/add', { template: 'Adding a product' } )
.when( '/other', { template: 'Other pages' } )
.otherwise( { redirectTo: '/product' } );
});
app.controller( 'MainCtrl', function ( $scope ) {
});
for more https://jsfiddle.net/zahiruldu/jLdce3vj/186/
You can use UI Router for handling parent routing properly that will give you advance nested route facilities.

Pass value to a template pages using Angular

I'm pretty new in Angular since today. :)
I've followed a lot of tutorials since this morning and for each one the same question from my side without answer.
I saw that my navigation could be as simple as the following:
<h3>HTML</h3>
<ul class="nav navbar-nav navbar-right">
<li>Home</li>
<li>About</li>
<li>Contact</li>
</ul>
JS
scotchApp.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'
});
});
But how it works if I need to page some value to a template page ?
And how can I get this passed value to use it in my template page ?
Thanks for your help.
Simply
define variable in controller $scope.variablename=value;
access in template as variablename inside double quotes or access as {{variablename}} outside the tag.
Don't forget to inject $scope dependency in controller.
Its all about using REST.
So lets say you want to pass a email id to your contact page, this is how your code would change.
//change in your html link. I have hard coded email for convenience. You could get it from $scope
<li>Contact</li>
Then you have to add the variables in your REST URL
//changes in your app.js
// route for the contact page
.when('/contact', {
templateUrl : 'pages/contact.html/:emailId',
controller : 'contactController'
});
And finally changes in your controller.
You'll have to add dependency for "$stateParams" and then the variable value will be available in your controller
scotchApp.controller ('contactController',['$stateParams', function($stateParams){
var emailId = $stateParams.emailId;
]})

Is it possible to load ng-view automatically?

I would like to that way, if it exists, how to do that, ng-view (or ui view) loads, when load the page. For example: I would like to seperate header files, menu files, etc. So the Webpage should stands some views. I found only tutorials when views load after a click. Please inform me about this.
Thank you.
When you build your router you specify which template to show for every route. You can also create a default one like so:
.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'
})
.otherwise({redirectTo: '/'});
});

AngularJS nested states using stateProvider

I am trying to implement nested states using stateProvider. Facing issues while loading the nested states using url-routing. I have created two independent states and 2 nested states for one of the inndependent state. Please check the state configuration below:
.state('state1',{
url : "/page1",
templateUrl : "/views/page1.html",
contoller : 'page1ctrl'
})
.state('state2', {
url : "/page2",
templateUrl : "/views/page2.html",
controller : 'page2ctrl'
})
state('state2.nestedstate1', {
url : "/:nestedstate1", //passing as parameter
templateUrl : "/views/temp1.html",
controller : 'page2ctrl'
})
.state('state2.nestedstate1.nestedstate2', {
url : "/nestedstate2/:param1/:param2",
templateUrl : "/views/temp2.html",
controller : 'ctrl'
})
Issue : If I try to load complete page directly using complete url index.html/page2/nestedstate1/nestedstate2/fname/lname, it will first load data from last child state nestedstate2 and then fall back to its parent state 'nestedstate1' and also updates the url to index.html/page2/nestedstate1.
Required behaviour is to execute parent state first then the child state. For Example, nestedstate1 is necessary to load before nestedstate2.
Please suggest if I am missing any configuration.
Thanks
I created working example here. It is 1:1 to your scenario.
In the above script I found only one typo:
// missing dot
state('state2.nestedstate1', {
// should be
.state('state2.nestedstate1', {
The example is then working, while using these calls:
<a ui-sref="state1">
// ui-sref
<a ui-sref="state2">
<a ui-sref="state2.nestedstate1({nestedstate1: 'theNestedStateA'})">
<a ui-sref="state2.nestedstate1.nestedstate2({
nestedstate1: 'theNestedStateB', param1: 'value1' ,param2: 'value2'})">
// href
<a href="#/page2">
<a href="#/page2/nestedstate0">
<a href="#/page2/nestedstate1/nestedstate2/fname/lname">
All the rest should be almost the same. You can compare this working version with your local code, to find out what is wrong...
Check it here
Extend
Each view in (state2 chain) is provided with its own controller
.state('state2', {
url : "/page2",
templateUrl : "/views/page2.html",
controller : 'page2ctrl'
})
.state('state2.nestedstate1', {
url : "/:nestedstate1", //passing as parameter
templateUrl : "/views/temp1.html",
controller : 'page2Nestedctrl'
})
.state('state2.nestedstate1.nestedstate2', {
url : "/nestedstate2/:param1/:param2",
templateUrl : "/views/temp2.html",
controller : 'ctrl'
})
And they are like this:
.controller('page2ctrl', ['$scope', function ($scope) {
console.log('page2ctrl')
}])
.controller('page2Nestedctrl', ['$scope', function ($scope) {
console.log('page2Nestedctrl')
}])
.controller('ctrl', ['$scope', function ($scope) {
console.log('ctrl')
}])
Then when navigating to url: page2/nestedstate1/nestedstate2/fname/lname, we can see this in console:
page2ctrl
page2Nestedctrl
ctrl
And that should show, that all the states are initiated in expected order

Resources