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;
]})
Related
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.
I am using ng-route in angularjs to switch beteen views , I made it to work, sample code below:
Html:
Mappings
New Products
angularjs
.config(function ($routeProvider) {
$routeProvider
.when("/", {
templateUrl: "/MbfProduct/Main"
})
.when("/Mappings", {
templateUrl: "/Mappings"
})
.when("/Products", {
templateUrl: "/Products"
})
})
So everything is OK just I had to add the "#" in the ng-href attribute so the page doesn't get refreshed.
So my question how can I have the result, I mean no page refresh, without having the '#' in the href ?
you can write a function in your controller that changes the view. You have to use $location provider to switch between views. There is a method named path that does the switching.
Something like this.
app.controller("TestCtrl", function($scope, $location){
$scope.changeView = function(){
$location.path("/Mappings");
}
})
and call changeView function on ng-click of anchor tag and just remove the ng-href tag altogether. If that doesn't work you can use ng-href="javascript:void(0)" as well to give a void link to anchor tag.
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
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: '/'});
});
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