how do i display a page with side-menu using $location? - angularjs

i'm trying to display a page that has a side-menu. i've tried adding enable-menu-with-back-views="true" to my ion-side-menus but it still does not display the page.
here's my code for the side-menu:
<ion-side-menus enable-menu-with-back-views="true">
<ion-side-menu-content>
<ion-nav-bar class="bar-stable">
<ion-nav-back-button></ion-nav-back-button>
<ion-nav-buttons side="left">
<button class="button button-icon button-clear ion-navicon" menu-toggle="left"></button>
</ion-nav-buttons>
</ion-nav-bar>
<ion-nav-view name="side-menu21"></ion-nav-view>
</ion-side-menu-content>
<ion-side-menu side="left" id="side-menu21">
<ion-header-bar class="bar-stable">
<div class="title">Menu</div>
</ion-header-bar>
<ion-content padding="false" class="side-menu-left has-header ">
<ion-list id="menu-list1">
<ion-item id="menu-list-item1" ui-sref="menu.mainMenu" menu-close="">Main Menu</ion-item>
<ion-item id="menu-list-item2" ui-sref="menu.seeNearbyCICOs" menu-close="">See Nearby CICOs</ion-item>
<ion-item id="menu-list-item3" ui-sref="menu.termsAndConditions" menu-close="">Terms and Conditions</ion-item>
<ion-item id="menu-list-item4" ui-sref="menu.fAQ" menu-close="">FAQ</ion-item>
<ion-item id="menu-list-item5" ui-sref="menu.contactUs" menu-close="">Contact Us</ion-item>
<ion-item id="menu-list-item6" ui-sref="menu.help" menu-close="">Help</ion-item>
</ion-list>
</ion-content>
</ion-side-menu>
</ion-side-menus>
and here's my controller.js code:
.controller('loginCtrl', ['$scope', '$stateParams', '$location', function ($scope, $stateParams, $location) {
$scope.userdata = {}
$scope.enterlogin = function(usern,pass)
{
if(usern== "admin" && pass == "bam_1987!")
{
$location.path('/page19');
}
else
{
alert('Please check your credentials.');
}
}
}])
where /page19 is the url of state menu.help, the page i'm trying to display.

I don't know much about $location, but maybe you have to use this :
$location.path("yourpath")
$scope.$apply()
if you get an error relative to $digest when you use this line , try :
$location.path("yourpath")
setTimeout(function(){$scope.$apply()})
Or , you can use states with ui.router , and then use :
$state.go("yourState")
This is what i use to change pages

Related

ionic - unable to print title on navbar

I am working with angular and ionic and i'm currently not able to print ANY title in the navbar.
I have a quite complex setup with multiple nested views.
Here's the list of states:
state('app', {
cache: false,
url: '/app',
abstract: true,
templateUrl: './sections/menu/menu.tpl.html',
controller: 'menuCtrl'
}).state('app.home', {
url: '/home',
views: {
'appContent': {
templateUrl: './sections/menu/pageViewsContainer.tpl.html',
controller: 'homeCTRL'
},
'Details#app.home': {
templateUrl: './sections/home/home.tpl.html'
},
'List#app.home': {
templateUrl: './sections/List/List.tpl.html'
}
}
}).state('app.details', {
name: 'appDetails',
url: '/:ID',
views: {
'appContent': {
templateUrl: './sections/menu/pageViewsContainer.tpl.html',
controller: 'DetailsCtrl'
},
'zoneDetails#app.details': {
templateUrl: './sections/Details/Details/details.tpl.html'
},
'zoneList#app.details': {
templateUrl: './sections/List/List.tpl.html'
}
}
});
My main (abstract) state is app and uses menu.tpl.html which looks like this:
<ion-side-menus enable-menu-with-back-views="true">
<ion-side-menu-content class="custom-central-content">
<ion-nav-bar class="bar-calm custom-header-bar">
<ion-nav-buttons side="left">
<!--<button class="button button-icon button-clear ion-home" menu-toggle="left"></button>-->
<button class="button button-icon button-clear ion-home" ui-sref="app.myHouse" id="AppMenuLeftIcon"></button>
</ion-nav-buttons>
<ion-nav-buttons side="right">
<button class="button button-icon button-clear ion-navicon" menu-toggle="right" id="AppMenuRightIcon"></button>
</ion-nav-buttons>
</ion-nav-bar>
<ion-view ui-view="appContent"></ion-view>
</ion-side-menu-content>
<ion-side-menu side="right">
<ion-header-bar class="bar-dark">
.....
inside <ion-view ui-view="appContent"></ion-view> I am using the template from pageViewsContainer.tpl.html which is:
<ion-view>
<ion-content class="hg-split-page-container">
<div ui-view="List" class="hg-split-page-zone-list has-header" nav-transition="none"></div>
<ion-nav-view name="Details" class="hg-split-page-zone-details"></ion-nav-view>
</ion-content>
</ion-view>
and then inside both Details and List I am printing the two views I need.
The problem is that I need to be able to set a static title for the main (home) page, but then dynamically change it when I go inside Details with the name of the page from my scope.
The problem is that I am not able to print ANY title (plain text, not scope) in any way.
I tried with (in pageViewsContainer.tpl.html):
<ion-view>
<ion-nav-title>View 1</ion-nav-title>
<ion-content class="hg-split-page-container">
<div ui-view="List" class="hg-split-page-zone-list has-header" nav-transition="none"></div>
<ion-nav-view name="Details" class="hg-split-page-zone-details"></ion-nav-view>
</ion-content>
</ion-view>
<ion-view view-title="View 1">
<ion-content class="hg-split-page-container">
<div ui-view="List" class="hg-split-page-zone-list has-header" nav-transition="none"></div>
<ion-nav-view name="Details" class="hg-split-page-zone-details"></ion-nav-view>
</ion-content>
</ion-view>
as well as (inside details template):
<ion-view>
<ion-nav-title>View 1</ion-nav-title>
<ion-content class="has-footer has-header">
...
<ion-view view-title="View 1">
<ion-content class="has-footer has-header">
...
But nothing worked.
Suggestions?
Try adding the title command inside header-bar
<ion-view>
<ion-header-bar class="bar-stable">
<h1 class="title">View 1</h1>
</ion-header-bar>
<ion-view>
or try
<ion-view>
<div class="bar bar-header browse-header" align-title="center">
<h1 class="title">View 1</h1>
</div>
<ion-content class="has-header">
</ion-content>
</ion-view>
It turned out I had to change quite a bit on my navigation, as I was using the nested views, parent, childs in a completely wrong way. It's working fine now.
Also, if, for some reasons, the title navbar it's still not displayed, you can force it using this in the controller (coffeescript):
# This should show the navabar everytime the view is entered
$scope.$on '$ionicView.enter', () ->
$ionicNavBarDelegate.showBar true

Ionic routing to sub-page

I'm having trouble setting up ionic to do proper routing. Want it to upen another page and have the back button displayed.
routes.js
angular.module('app.routes', [])
.config(function($stateProvider, $urlRouterProvider) {
// Learn more here: https://github.com/angular-ui/ui-router
$stateProvider
.state('records', {
url: '/records',
templateUrl: 'templates/records.html',
controller: 'RecordsCtrl'
})
.state('records-newRecord', {
url: '/records/newRecord',
templateUrl: 'templates/newRecord.html',
controller: 'newRecordCtrl'
})
$urlRouterProvider.otherwise('/records')
});
records.html
<ion-view title="Records" id="main">
<ion-nav-bar>
<!-- ADD BUTTON-->
<ion-nav-buttons side="primary">
<button class="button button-icon ion-plus-circled" href="#/records/newRecord"></button>
</ion-nav-buttons>
<!--SEARCH BUTTON-->
<ion-nav-buttons side="secondary">
<button class="button button-icon ion-search" ng-click="toggleSearchBar()"></button>
</ion-nav-buttons>
</ion-nav-bar>
<ion-content padding="true" class="has-header">
<!-- SEARCH BAR -->
<div ng-show="showSearchBar">
<label class="item item-input">
<i class="icon ion-search placeholder-icon"></i>
<input type="search" placeholder="">
</label>
</div>
<!-- LIST OF RECORDS -->
<form class="list">
<ion-item class="item-thumbnail-left">
<img src="img/pQqcmU4fR8GSvP092hQN_Lockreal72.jpg">
<h2>Temp. Logger PC4</h2>
<p>SN C001517 A</p>
</ion-item>
</form>
</ion-content>
</ion-view>
The problem is that it does not redirect.
I had another version that did, but it didn't show the back button or if it did then the header was empty. So changed the code to what it is now.
What am I doing wrong here?
BTW: Is there a way to prompt the user if the back button is pressed. Like prompting and asking "Sure you wanna leave?"
On the new Record page try adding the ion-nav-back-button markup in ion-nav-bar that should add a backbutton you can also check the docs for ways to customise the button
<ion-nav-bar>
<ion-nav-back-button>
</ion-nav-back-button>
</ion-nav-bar>
and about adding a prompt you will need to use the custom method instead
Template
<ion-nav-bar ng-controller="MyCtrl">
<ion-nav-back-button class="button-clear"
ng-click="myGoBack()">
<i class="ion-arrow-left-c"></i> Back
</ion-nav-back-button>
</ion-nav-bar>
Controller
function MyCtrl($scope, $ionicHistory, $ionicPopup) {
$scope.myGoBack = function () {
$ionicPopup.confirm({
title: 'Leaving',
template: 'Are you sure you want to leave?'
}).then(function (res) {
if (res) {
$ionicHistory.goBack();
} else {
console.log('You are not sure');
}
});
};
}
I found the that using the ui-sref= is a simple solution from this post.
<ion-view view-title="Records" id="main">
<ion-nav-bar class="bar-stable">
<!-- ADD BUTTON-->
<ion-nav-buttons side="primary">
<button class="button button-icon ion-plus-circled" ui-sref="newrecord"></button>
</ion-nav-buttons>
...
Made the routes.js more simple
angular.module('app.routes', [])
.config(function($stateProvider, $urlRouterProvider) {
// Learn more here: https://github.com/angular-ui/ui-router
$stateProvider
.state('records', {
url: '/records',
templateUrl: 'templates/records.html',
controller: 'RecordsCtrl'
})
.state('newrecord', {
url: '/newrecord',
templateUrl: 'templates/newRecord.html',
controller: 'newRecordCtrl'
})
$urlRouterProvider.otherwise('/records')
});

Side menu appears only after refresh

I want to create a mobile app with Ionic framework (my first one). I've created expense.html and expense-detail.html. The sidemenu appears correctly on the expense.html but on the "child" page it appears only after refresh. Here is my code:
This is my side-menu:
<ion-side-menus>
<ion-side-menu-content>
<ion-header-bar class="bar-header bar-dark">
<button class="button button-icon icon ion-navicon" menu-toggle="left"></button>
<div class="h1 title">My Money Tracker</div>
</ion-header-bar>
<ion-nav-view name="mainContent" animation="slide-left-right"></ion-nav-view>
<ion-content></ion-content>
</ion-side-menu-content>
<ion-side-menu side="left">
<a menu-close ui-sref="app.expenses" class="item">Expenses</a>
<a menu-close ui-sref="app.incomes" class="item">Incomes</a>
<a menu-close ui-sref="app.categories" class="item">Categories</a>
<a menu-close ui-sref="app.statistics" class="item">Statistics</a>
<a menu-close ui-sref="app.account" class="item">Account</a>
</ion-side-menu>
</ion-side-menus>
These are the states of the application:
.state('app.expenses', {
url:"/expenses",
views: {
"mainContent" : {
templateUrl : "app/expenses/expenses.html"
}
}
})
.state('app.expense-detail', {
url:"/expenses/:id",
views: {
"mainContent" : {
templateUrl : "app/expenses/expense-detail.html"
}
}
})
The sidemenu appears on all of app.* states by default except for app.*-detail in which it appears only after a refresh.
What am I doing wrong?
I solved this problem by reading this http://ionicframework.com/docs/api/directive/menuToggle/ and adding enable-menu-with-back-views="true" to the side menu

Sharing multiple views and controllers using "ion" view calls in Ionic Project

I've been stuck on this issue for a week now and I feel like I understood 'ui-router' and 'ionic's' view routing but this seems not to be the case.
I've seen numerous examples how the view works but now as my build is larger, the examples I've seen are too simplistic for my project as my project has views for both a side menu, tabs, and each tabs content.
My issue:
I am holding more than one view with different controllers inside of my tabs. I'm working with a leaflet map and a drop down list that will be populated with my locations and I got both to work. However, the views are making weird calls. Anytime I select the leaflet map it pushes my "Discover List" (in my discover-home.html) button under my apps ion-nav-bar in my menu.html. In addition, you can see that this list view is in-between my map and the zoom icons and if do not touch my map and I switch tabs and come back I cannot open the list again.
I think the question's I need to ask to understand my problem are
1. What is the correct way to navigate in a multi-viewed applications using ionic's view navigation?
2. Why does changing views disable other controllers from being called again?
3. What are some of the best practices?
This is what I'm dealing with. Any help would be appreciated.
my tab view inside discover-home.html
Sidebar view inside menu.html
List Display inside discover-home.html
discover list in discover-home.html hidden under ion-nav-bar in menu.html
Here's my code snippets
index.html
<body ng-app="starter">
<ion-nav-view></ion-nav-view>
</body>
discover-tabs-controller.html
This controls the tabs views (one of which is discover-home.html)
<ion-view>
<div class="tabs-striped tabs-top tabs-background-positive tabs-color-light">
<ion-tabs class="tabs-positive tabs-icon-only" >
<ion-content has-subheader="true"></ion-content>
<!--HOME TAB [OPEN]-->
<ion-tab title="Discover" icon-on="icon ion-home" icon-off="icon ion-home"
href="#/app/discover/home">
<ion-nav-view cache-view="true" name="home-tab"></ion-nav-view>
<!-- View For Home.html -->
</ion-tab>
<!--HOME TAB [CLOSED]-->
<!--MORE TABS HERE-->
</ion-content>
</ion-tabs>
</div>
</ion-view>
Discover-home.html
<ion-view view-title="Home">
<!--SUBHEADER BUTTON: DISPLAY LISTVIEW -->
<div ng-controller="FrostCtrl" class="bar bar-subheader button bar-balanced" ng-click="pushFrost()">
<h2 class="title">{{title}} List</h2>
</div>
<!--DISPLAY OVERLAY WITH LIST-->
<ion-pane ng-controller="OverlayCtrl" class="dark-overlay" ng-show="showOverlay">
<ion-content class="has-subheader">
<button class="button button-block button-outline button-balanced" ng-click="hideFrost()">Dismiss
</button>
<ion-list>
<ion-item ng-repeat="item in items" class="dark-item">
{{item.text}}
</ion-item>
</ion-list>
</ion-content>
</ion-pane>
<!--LEAFLET MAP -->
<leaflet class="has-subheader padding"center="nassau" paths="paths" tiles="tiles" markers="markers" defaults="defaults">
</leaflet>
</ion-view>
controller.js
angular.module('starter.controllers', [])
...
// #######| LEAFLET MAP |#######
.controller('ActivityCntl', [ '$scope', 'leafletData', function($scope, leafletData) {
var tileSource = {
onlineTiles: {
url: "http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
},
};
angular.extend($scope, {
nassau: {
lat: 25.074521,
lng: -77.318191,
zoom: 13
.........
});
}])
// #######| SHOW OVERLAY |#######
.controller('FrostCtrl', ['$scope', '$rootScope', '$compile', function($scope, $rootScope, $compile) {
$scope.pushFrost = function() {
var el = angular.element(document.getElementById('myPane'));
el.attr('frost', '');
el = $compile(el)($scope);
$rootScope.showOverlay = true;
};
}])
//#######| DISPLAYS CONTENTS |##########
.controller('OverlayCtrl', function($scope, $rootScope, $compile) {
$scope.items = [];
for(var i = 0; i < 5; i++) {
$scope.items.push({
text: 'Whatever ' + (i+1)
});
}
$scope.hideFrost = function() {
$rootScope.showOverlay = false;
var el = angular.element(document.getElementById('myPane'));
};
})
app.js
config(['$stateProvider', '$urlRouterProvider','$locationProvider',function($stateProvider, $urlRouterProvider, $locationProvider) {
$stateProvider
.state('app', {
name: 'app',
url: "/app",
abstract: true,
templateUrl: "templates/menu.html",
controller: 'AppCtrl'
})
.state('app.discover', {
name: 'app.discover',
url: "/discover",
views: {
'menuContent': {
templateUrl: "templates/discover-tabs-controller.html"
}
}
})
// for my discover-home.html
.state('app.discover.home', {
url: "/home",
views: {
'home-tab': {
templateUrl: "templates/discover-tabs/discover-home.html",
controller: 'ActivityCntl'
},
'discover-home-listview': {
templateUrl: "templates/discover-tabs/discover-home.html",
}
}
})
menu
This controls the sidemenu items
<ion-side-menus enable-menu-with-back-views="false">
<ion-side-menu-content>
//IT GETS PUSHED UNDER THIS
<ion-nav-bar class="bar-calm">
<ion-nav-back-button>
</ion-nav-back-button>
<ion-nav-buttons side="left">
<button class="button button-icon button-clear ion-navicon" menu-toggle="left">
</button>
</ion-nav-buttons>
</ion-nav-bar>
<ion-nav-view name="menuContent"></ion-nav-view>
</ion-side-menu-content>
<ion-side-menu side="left">
<ion-header-bar class="bar-stable">
<h1 class="title">MYApps</h1>
</ion-header-bar>
<ion-content>
<ion-list>
<ion-item menu-close ng-click="login()">
<i class="icon ion-person"></i>
Login
</ion-item>
<ion-item menu-close href="#/app/discover">
<i class="icon ion-location"></i>
Discover
</ion-item>
<ion-item menu-close href="#/app/map">
<i class="icon ion-map"></i>
Map
</ion-item>
</ion-list>
</ion-content>
</ion-side-menu>
</ion-side-menus>

Ionic app with side-menu and two views with tabs don't switch correctly

In my application with a side menu and several pages. One of those pages, (let's call it Page A) has tabs, everything was working OK - side menu changes the screen with different pages, Page A with tabs had it's tabs working.
Things went south when I tried to add another page with tabs (let's call it Page B). If I am on any page and click on Page A, Page A appears, its tabs works ok. If I click on side menu to open Page B, the URL changes, but nothing happens (Page A is still on screen). All other pages load without problems. If I refresh the browser with Page B URL opened, or refresh on any page other than Page A and then click on Page B side menu link, Page B loads OK, and the reverse happens - any page but Page A will load.
If I comment the code to have tabs on either Page A or Page B, everything works again. So it seems that I can't have an app with side menu and two different pages with different tabs. Is that correct?
Here's my code (relevant parts):
index.html:
<body ng-app="kinofit">
<ion-nav-view></ion-nav-view>
</body>
app.js:
angular.module('kinofit', ['ionic'])
.config(function($stateProvider, $urlRouterProvider) {
$stateProvider
.state('app', {
url: "/app",
abstract: true,
templateUrl: "templates/menu.html",
controller: 'AppCtrl'
})
.state('app.home', {
url: "/home",
views: {
'menuContent': {
templateUrl: "templates/home.html",
controller: 'HomeCtrl'
}
}
})
.state('app.aerobico', {
url: "/aerobico",
views: {
'menuContent': {
templateUrl: "templates/aerobico.html",
controller: 'AerobicoCtrl'
}
}
})
.state('app.musculacao', {
url: "/musculacao",
views: {
'menuContent': {
templateUrl: "templates/musculacao.html",
controller: 'MusculacaoCtrl'
}
}
});
templates/menu.html:
<ion-side-menus enable-menu-with-back-views="false">
<ion-side-menu-content>
<ion-nav-bar class="bar-balanced">
<ion-nav-back-button>
</ion-nav-back-button>
<ion-nav-buttons side="left">
<button class="button button-icon button-clear ion-navicon" menu-toggle="left">
</button>
</ion-nav-buttons>
</ion-nav-bar>
<ion-nav-view name="menuContent"></ion-nav-view>
</ion-side-menu-content>
<ion-side-menu side="left">
<ion-content>
<ion-list>
<ion-item nav-clear menu-close href="#/app/home">
<i class="icon ion-home"></i> Início
</ion-item>
<ion-item nav-clear menu-close href="#/app/aerobico">
<i class="icon ion-heart"></i> Aeróbico
</ion-item>
<ion-item nav-clear menu-close href="#/app/musculacao">
<i class="icon ion-checkmark-circled"></i> Musculação
</ion-item>
</ion-list>
</ion-content>
</ion-side-menu>
</ion-side-menus>
templates/musculacao.html:
<ion-view view-title="MUSCULAÇÃO" class="musculacao-view">
<ion-tabs tabs-type="tabs-icon-top" class="tabs-striped">
<ion-tab title="Tab 1">
<ion-content class="tabbed-content-within-sidemenu-app padding">
(...)
</ion-content>
</ion-tab>
<ion-tab title="Tab 2">
<ion-content class="tabbed-content-within-sidemenu-app padding">
(...)
</ion-content>
</ion-tab>
</ion-tabs>
</ion-view>
and templates/aerobico.html is the same, changing the view-title and class attributes.
I'm aware that codepen or similar websites would be better to post the code, but I couldn't make it work with multiple html files.
Any help would be greatly appreciated. Thanks.
Seems like problem related to caching of views, views Caching is by default enable in ionic framework
You need to Disable all caching by setting it to 0, before using it add $ionicConfigProvider dependency.(Do add it in config block)
$ionicConfigProvider.views.maxCache(0);
Similar answer here
OP Edit: I couldn't get this to work, but adding cache:false to the $stateProvider call (solution #2 on the "answer here" link above) did the trick for me (OP).

Resources