I have my routing defined as below
$stateProvider
('MasterPage', {
url: '/',
templateUrl: 'views/master.html',
})
.state('MasterPage.dashboard', {
url: 'dashboard',
templateUrl: 'views/dash.html',
})
.state('MasterPage.test', {
url: 'test/:id',
templateUrl: 'views/test.html',
})
$urlRouterProvider.otherwise('/');
I have links that allows me to navigate from mainpage to dashboard and then to the test page which works fine. When I navigate to the test page with an id:1234, which makes my url as <HOST>/#/test/1234, and try to refresh it fails. It gives me an error saying:
Cannot resolve state 'MasterPage.test/1234' from 'MasterPage.test'
Why is UI-Router considering the url segment as a state?
Update
I dont have any ui-sref. I am going to the test page by using $state.go('MasterPage.test', {id:1234}).
There is working plunker
This issue is usually related to wrong ui-sref setting
<a ui-sref="MasterPage.test/1234">
we need to split and pass 1) the state name, and 2) state params like this:
<a ui-sref="MasterPage.test({id:1234})">
In case, we want to use href, we just have to concatenate parent and child url definitions (parent '/', child 'test/:id') :
<a href="/test/1234">
EXTEND
So, these states (almost unchanged):
.state('MasterPage', {
url: '/',
templateUrl: 'views/master.html',
})
.state('MasterPage.dashboard', {
url: 'dashboard',
templateUrl: 'views/dash.html',
})
.state('MasterPage.test', {
url: 'test/:id',
templateUrl: 'views/test.html',
})
will work with these links
<a href="#/">
<a href="#/dashboard">
<a href="#/test/1234">
<a href="#/test/9875">
<a ui-sref="MasterPage">
<a ui-sref="MasterPage.dashboard">
<a ui-sref="MasterPage.test({id:2222})">
<a ui-sref="MasterPage.test({id:4444})">
There is working plunker
Related
I'm using angular ui router and routing my pages, here my main page is contracts.html
I want to route to another page from this page hence I have written the following code. But it is not working, please take a look at the following code :
<li role="menuitem"><a ui-sref=".monthlysettings" onclick="$('#simple-btn-keyboard-nav2').text('PDC Settings')">PDC Settings</a></li>
.state('contracts', {
url: '/contracts',
controller: 'contractsController',
templateUrl: 'contracts.html'
}).state('contracts.monthlysettings', {
url: '/monthlysettings',
controller: 'monthlysettingsController',
templateUrl: 'monthlysettings.html'
})
Your state name should be the full name contracts.monthlysettings
<li role="menuitem"><a ui-sref="contracts.monthlysettings" onclick="$('#simple-btn-keyboard-nav2').text('PDC Settings')">PDC Settings</a></li>
I've been working with Angular for a year or 2 now, but this is my first project using ui-router. I'm running into a few issues with views and sub-views. The app is a standard left-side menu bar, with the views on the right changing depending on what's clicked in the menu bar.
On index.html
<body>
<div ui-view></div>
</body>
In the config.js file, which defines the routes
.state("dashboard", {
url: "/dashboard",
templateUrl: "components/dashboard/dashboard.html",
data: {
pageTitle: "Dashboard",
requiresLogin: false
}
})
.state("dashboard.welcome", {
url: "/welcome",
templateUrl: "components/welcome/welcome.html",
data: {
pageTitle: "Welcome",
requiresLogin: false
}
})
In the dashboard.html file
<div class="dashboard">
<div class="container-fluid">
<div class="row">
<div class="col-xs-12 col-md-8">
<div ui-view>
The /dashboard path loads correctly, and will load the left-side navigation bar with a blank right side. But changing the state to dashboard.welcome (/welcome) will not load the welcome.html template.
Whenever working with ui-router you need to understand that the concept of states is different from routes. When you define a sub-state, its defined relative to its parent state. In your scenario dashboard.welcome is defined as a child state of dashboard. The routes to substate is relative to the parent and is {parent url}/{child url}. Hence you should use either of the below 2 to route to that state:
Using $state.go change the state by specifying state name
$state.go('dashboard.welcome');
Using $location.path change the route by specifying url
$location.path('/dashboard/welcome');
It sounds like you want links to /welcome to be for state dashboard.welcome. Here is a plunker showing how this can be done. I show two sets of dashboard and welcome states. The first set of states (dashboard & welcome) shows that /dashboard/welcome will bring you to the dashboard.welcome state.
The second set (dashboard2 & welcome2) shows that /welcome will go to state dashboard2.welcome2. I believe this is what you were looking for.
If you hover over the links you can see where they will take you.
https://plnkr.co/edit/AVKPFa?p=info
Nested routes in ui-router get nested urls. I would however recommend using named-views for this kind of structure. You can find more info about it here:
https://github.com/angular-ui/ui-router/wiki/Multiple-Named-Views
The gist of it is: you can specify a named component (ui-view) for your left menu navigation and another one for content, which gives you much more control down the line, because named components can be overwritten in child states or they can keep the default template, depending on your needs.
Example:
.state('root', {
url: '',
abstract: true,
views: {
'header': {
templateUrl: 'templates/partials/header.html',
controller: 'headerCtrl'
},
'logo': {
templateUrl: 'templates/partials/logoView.html'
},
'footer':{
templateUrl: 'templates/partials/footer.html',
controller: 'footerCtrl'
}
}
})
.state('root.login', {
url: '/login',
views: {
'header#': {
template: ''
},
'container#': {
templateUrl: 'templates/login.html',
controller: 'loginController'
}
}
})
.state('root.report', {
url: '/report',
views: {
'container#': {
templateUrl: 'templates/eu_dashboard.html',
controller: 'reportController'
}
}
})
And in your index.html:
<div ui-view="logo"></div>
<div ui-view="header"></div>
<div id="mainView" ui-view="container"></div>
<div ui-view="footer"></div>
So I've been running into this pretty dumb problem with states lately. I am creating my states with the parent.child state as so:
$stateProvider
.state('home', {
url: '/home',
templateUrl: 'app/views/pages/home.html',
})
.state('home.signup', {
templateUrl: 'app/views/pages/signup.html',
url: '/signup',
controller: "signupController"
})
.state('home.login', {
controller: "loginController",
templateUrl: 'app/views/pages/login.html',
url: '/login'
})
My html is as follows:
<a ui-sref="home.login"><i class="fa fa-sign-out fa-fw"></i> Login</a>
<a ui-sref="home.signup"><i class="fa fa-sign-out fa-fw"></i> Signup</a>
The initial parent state was dashboard instead of home. So dashboard.signup and dashboard.login. I had recently changed this to home.login and home.signup. All works when I go to /home/login and /home/signup, but click on my login link and signup links I get the following error:
Could not resolve 'dashboard.login' from state 'home'
I think its still reading the dashboard parent but I have nothing that even relates to it with my login and signup. Everything looks right to me. Any help would be appreciated.
can you check $state.go,what is the state you mentioned in your controller.
I am using ui-router and state provider to route the pages in my application. I have following states written in state provider.
.state('home', {
url: '/home',
templateUrl: 'public/msStream/stateFiles/stateHome.html',
controller: 'stateHomeController'
})
.state('home.profile', {
url: '/home/profile',
templateUrl: 'public/msStream/views/setting/ProfileBody.html'
})
When I am in home state. /home is added in my URL, but when I switch to home.profile state using $state.go("home.profile), my URL is not changing to /home/profile but HTML page added in templateurl of the same state is getting rendered on front.
I tried adding /profile and /home/profile in the URL of the state but nothing seems to work. What am I missing here?
I created working plunker here
The paren-child states do inherit a lot. Among other things, also the url. So we should not use for child the url part coming from parent.
$stateProvider
.state('home', {
url: '/home',
templateUrl: 'public/msStream/stateFiles/stateHome.html',
controller: 'stateHomeController'
})
.state('home.profile', {
// HERE
// instead of this
// url: '/home/profile',
// we need this
url: '/profile',
templateUrl: 'public/msStream/views/setting/ProfileBody.html'
})
Also very improtant note - parent must contain anchor/target/ui-view for its child:
public/msStream/stateFiles/stateHome.html
<div>
<h2>this is the home</h2>
placeholder for child:
<hr />
<div ui-view=""></div>
</div>
The most important here is the <div ui-view=""></div> - a placeholder for child view (unnamed)
Check it in action here
Hi I have two separate templates named "home.html" & "home.singleLink.html" in templates folder.
home.html looks like this
<ion-list>
<ion-item ng-repeat="link in links">
<a class="button button-clear" ui-sref="home({singleLink: link.name})">{{link.name}}</a>
</ion-item>
<ion-list>
And I want to show "home.singleLink.html" template and vanishes the "home.html" template when a user click on the link what is in ui-sref. I am passing a variable after the "home"
Here is my app.config looks like
$stateProvider
.state('home', {
url: '/home',
templateUrl: 'templates/home.html',
controller: 'linksController'
})
.state('home.singleLink', {
url: '/:singleLink',
templateUrl: 'templates/home.singleLink.html',
controller: 'linksController'
})
As per I know ui-sref will generate a href when a user click in the link. But in my case when I inspect the link I can see a additional href="#/home" along with the ui-sref which is not changing when I click in the link.
Thanks in advance.
We have to change to things. I created working plunker here.
Firstly, the state call:
<ion-list>
<ion-item ng-repeat="link in links">
<a class="button button-clear"
ui-sref="home.singleLink({singleLink: link.name})">{{link.name}}</a>
</ion-item>
</ion-list>
As we can see, the ui-sref is now different
// instead of this:
ui-sref="home({singleLink: link.name})"
// we have to use full state name home.singleLink
ui-sref="home.singleLink({singleLink: link.name})"
Secondly the child state view target
.state('home.singleLink', {
url: '/:singleLink',
views: {
'#': {
templateUrl: 'templates.home.singleLink.html',
controller: 'linksController'
}
}
})
Because our list view (state 'home'), does not have target for child home.singleLink, we have to use absolute naming and place it into root:
views: {
'#': {
Find more here:
View Names - Relative vs. Absolute Names
Behind the scenes, every view gets assigned an absolute name that follows a scheme of viewname#statename, where viewname is the name used in the view directive and state name is the state's absolute name, e.g. contact.item. You can also choose to write your view names in the absolute syntax.
For example, the previous example could also be written as:
.state('report',{
views: {
'filters#': { },
'tabledata#': { },
'graph#': { }
}
})
Check it here, Similar stuff here: Resolving promise with ionic/ui-routing
Since your templates are named home.html and home.singleLink.html I am assuming singleLink is a child of home. Your config should look like this.
$stateProvider
.state('home', {
url: '/home',
views: {
'home': {
templateUrl: 'templates/home.html',
controller: 'linksController'
}
}
})
.state('home.singleLink', {
url: '/singleLink',
views:{
'singleLinkView': {
templateUrl: 'templates/home.singleLink.html',
controller: 'linksController'
}
}
And your home.html shoul look like this
<ion-list>
<ion-item ng-repeat="link in links">
<a class="button button-clear" ui-sref="home.singleLink">{{link.name}}</a>
</ion-item>
<ion-list>