Ui-sref is not generating the right url in ionic framework - angularjs

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>

Related

Navigate from one tab to a nested view of another tab

Is it possible to link from one tab to a nested view in another tab? I've tried two different methods and neither seem to work.
Here's my router config:
.config(function($stateProvider, $urlRouterProvider) {
// Ionic uses AngularUI Router which uses the concept of states
// Learn more here: https://github.com/angular-ui/ui-router
// Set up the various states which the app can be in.
// Each state's controller can be found in controllers.js
$stateProvider
// setup an abstract state for the tabs directive
.state('tab', {
url: '/tab',
abstract: true,
templateUrl: 'templates/tabs.html'
})
// Each tab has its own nav history stack:
.state('tab.dash', {
url: '/dash',
views: {
'tab-dash': {
templateUrl: 'templates/tab-dash.html',
controller: 'DashCtrl'
}
}
})
.state('tab.chats', {
url: '/chats',
views: {
'tab-chats': {
templateUrl: 'templates/tab-chats.html',
controller: 'ChatsCtrl'
}
}
})
.state('tab.chat-detail', {
url: '/chats/:chatId',
views: {
'tab-chats': {
templateUrl: 'templates/chat-detail.html',
controller: 'ChatDetailCtrl'
}
}
})
// if none of the above states are matched, use this as the fallback
$urlRouterProvider.otherwise('/tab/dash');
});
In templates/tab-dash.html I have a link to a particular chat detail page:
<ion-view view-title="Dashboard">
<ion-content class="padding">
<h1>My Chats</h1>
Chat #1
</ion-content>
</ion-view>
From here I can navigate the chat detail page, however if I click the "Chats" tab button at the bottom nothing at all happens. I'd like it to bring me to the main Chats page.
Another method I tried was using ui-sref instead of href:
<ion-view view-title="Dashboard">
<ion-content class="padding">
<h1>My Chats</h1>
<a ui-sref="tab.chat-detail{chatId:1}">Chat #1</a>
</ion-content>
</ion-view>
In this case I receive an error Could not resolve 'tab.chat-detail{chatId:1}' from state 'tab.dash'
What's the best way to link to a "detail" view from within another tab? My main is to make sure that clicking the tab button at the bottom always brings me to the parent page for that tab. Right now, in the first example, it gets stuck on the "detail" view.
I got a workaround. I am hiding the tab bar when navigating to nested view of another tab.
And showing it again when user navigates back to the root tab.
To know how to hide and show tab bar programmatically, check this link:-
https://stackoverflow.com/a/45002644/4527624

UI-Router considering url as state

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

Angular ui-router subview issues

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>

Ionic back button not working even though ionicHistory back view exists

My ionic back button doesn't do anything when it is being clicked even though, when I print out the version history, it shows that there is a back view. Also for some reason $ionicHistory.enabledBack() is returning false, even though the back view exists and the back view and current view have the same id. Does anyone know how to fix the problem?
Here's a snapshot of the console.
Use ui-router nested views to create states that will replace an ion-nav-view tag in the HTML.
Use ion-view, not ion-nav-view, inside the template html for each nested view.
Example:
Parent (Nav) View HTML:
<ion-nav-view name="navView"></ion-nav-view>
Nested View Level 1 HTML:
<ion-view view-title="Nested View Level 1">
<ion-content></ion-content>
</ion-view>
Nested View Level 2 HTML:
<ion-view view-title="Nested View Level 2">
<ion-content></ion-content>
</ion-view>
ui-router:
$stateProvider
.state('app.navView', {
url: '/nav-view',
views: {
'menuContent': {
templateUrl: 'app/navView.html',
controller: 'NavViewCtrl as vm'
}
}
})
.state('app.nestedViewL1', {
url: '/nested-view-L1',
views: {
'navView': {
templateUrl: 'app/nestedViewL1.html',
controller: 'NestedView1CtrL1 as vm'
}
}
})
.state('app.nestedViewL2', {
url: '/nested-view-L2',
views: {
'navView': {
templateUrl: 'app/nestedViewL2.html',
controller: 'NestedViewL2Ctrl as vm'
}
}
})

Ionic - [ui.router] can't navigate nested states

I've been trying to make this work, I think I'm missing something but I don't know what. I've tried with absolute paths and same problem. Perhaps the way I'm trying to link the states?
Tree view goes like this:
- app
- app.category
- app.category.category-detail
I can navigate from app, to app.category but I can't go any further, here are my routes:
.state('app.category', {
url: "/category?catId",
views: {
'menuContent': {
templateUrl: "templates/category.html",
controller: "CategoryCtrl",
params: ['catId ']
}
}
})
.state('app.category.category-detail', {
url: "/category-detail",
views: {
'menuContent': {
templateUrl: "templates/category-detail.html",
controller: "DirectoryDetailCtrl",
params: [ 'catId' ]
}
}
})
This is my category.html
<ion-view>
<ion-nav-bar class="bar-dark nav-title-slide-ios7 bg-nav" ng-controller="NavCtrl">
<ion-nav-back-button class="button-clear" ng-click="myGoBack()">
<i class="ion-chevron-left blanco negrita"></i>
</ion-nav-back-button>
</ion-nav-bar>
<ion-content class="directory-content padding-3 bg-gris">
<ion-list>
<ion-item ng-repeat="category in categories" class="item-thumbnail-left tarjeta">
<a ui-sref="app.category.category-detail">
{{category.categoryName}}
</a>
</ion-item>
</ion-list>
</ion-content>
</ion-view>
Now, if I inspect this website with Chrome Dev Tools I can see the next link is generated:
<a ui-sref="app.category.category-detail" href="#/app/category/category-detail?catId=1" class="">
Category 1
</a>
But the view is not being loaded, and no error is shown.
category-detail.html
<ion-view>
<ion-nav-bar class="bar-dark nav-title-slide-ios7 bg-nav" ng-controller="NavCtrl">
<ion-nav-back-button class="button-clear" ng-click="myGoBack()">
<i class="ion-chevron-left blanco negrita"></i>
</ion-nav-back-button>
</ion-nav-bar>
<ion-content class="category-content padding-3 bg-gris">
<h1>CATEGORY</h1>
</ion-content>
</ion-view>
There is one issue with params definition and second with view/target naming
I. params: ['catId] issue
Read more about params: {} object for example here:
Angular ui router passing data between states without URL
Notation used above:
params: [ 'catId' ]
is not valid anymore... Now we have more settings arround, e.g. the default value:
params : { cateId : null }
read more here
II. view naming
It also seems, that you are trying to target the same ui-view="menuContent" from 'app.category' and its child state 'app.category.category-detail' ... both with relative naming:
.state('app.category', {
url: "/category?catId",
views: {
'menuContent': { // no # = relative name
...
.state('app.category.category-detail', {
url: "/category-detail",
views: {
'menuContent': {
...
In case, that this target ui-view="menuContent" is defined in the 'app' state, we have to use absolute naming
.state('app.category.category-detail', {
url: "/category-detail",
views: {
'menuContent#app': {
...
The reason is, that name view notation without '#' is realtive and could be expressed as 'viewName#parentState'. So these are the same
.state('app.category', {
url: "/category?catId",
views: {
// relative
'menuContent': {
// absolute - targeting the same ui-view
'menuContent#app': { // this is absolute name
Try to get more here (not about ionic, but the UI-Router stuff behind is the same):
Angularjs ui-router not reaching child controller
Angular UI Router - Nested States with multiple layouts

Resources