bug: $ionicView.leave doesn't fire with nested views.
Type: bug
Platform: all
If you have an app with a nested ion-nav-view, the "$ionicView.leave" event is not called on the child view. (It is called on the parent view) You have to resort to using "$destroy".
In the CodePen below, you can see that the child controllers "$ionicView.leave" event is not called.
CodePen:
http://play.ionic.io/app/6eafd1a2beb4
Try using $ionicNavView.leave
Doesn't seem to work - in fact if I put this in the PrivateChildCtrl:
$scope.$on("$ionicNavView.leave", function(){
console.log("IonicNavView leave event");
});
It gets called when entering (???) the child view after pressing Login.
I guess I should say that the event does fire - but maybe it's being caught and prevented from propogating by the parent view?
Okay, seeing this as well. I'll let @adamdbradley take a look at this.
+1, also experiencing this
Any progress?
@adamdbradley Hello, have you looked through this issue?
Also experiencing this.
- When I go to tab view inside one of menu items
$ionicView.enterfires correctly. - When I switch between tabs in this same view (same menu item)
$ionicView.leaveand$ionicView.enterare fired correctly. - If I switch to other menu item out of tab which needs to fire
$ionicView.leavenothing happens. HTML inspector shows that tabs stays in "active" state and don't go to cache or other inactive state. $ionicNavView.leavedoesn't help here (was discussed in other similar issue)
Short vid with output of leave events http://quick.as/bnbzir5j9
Code example is down here:
routing:
$stateProvider
.state('app', {
url: '/app',
abstract: true,
templateUrl: 'templates/menu.html',
})
.state('app.tabs', {
url: '/tabs',
abstract: true,
views: {
'menuContent': {
templateUrl: 'templates/tabs.html'
}
}
})
.state('app.tabs.tab1', {
url: "/tab1",
views: {
'report-tab': {
templateUrl: "templates/tabs/tab1.html",
controller: 'TabOneCtrl' // HERE we need to $ionicSideMenuDelegate.canDragContent(false);
// because we use some component that uses slide as well
}
}
})
.state('app.tabs.tab2', {
url: "/history",
views: {
'history-tab': {
templateUrl: "templates/tabs/tab2.html",
}
}
})
.state('app.some-view', {
url: '/some-view',
views: {
'menuContent': {
templateUrl: 'templates/some-view.html',
}
}
controller:
angular.module('app.controllers', [])
.controller('TabOneCtrl', function($scope, $ionicSideMenuDelegate, ...) {
/* controller code here */
// Disable menu swipe as it interferes with tab1 UX
$scope.$on('$ionicView.enter', function(){
$ionicSideMenuDelegate.canDragContent(false);
});
$scope.$on('$ionicView.leave', function(){
$ionicSideMenuDelegate.canDragContent(true);
});
});
You have a typo in your controller. It should be $ionicView.leave Not $ionicView.Leave. Lowercase "l"
@spectravp I'm sorry. this typo happened later after copying code here when I've tried beforeLeave and afterLeave.
leave doesn't work.
You don't catch me so easy :P
+1 also seeing $ionicView.leave and $destroy fired and caught not by the scope of the state that is being left, but by the previous state.
@demoalex same issue here. But more deeper... I have two tab views, same as you on menu switch the last view will stay active. After opening a view in the other tab view, all view events will fire in the last view from the first tab view... strange behavior. (To test change the url #/home/module1/entry3 to #/home/module2/entry3 )
See the console.log in this plunker: http://plnkr.co/edit/p4Uj1buIEH9YfqsNCEvN?p=preview
See this plunker http://plnkr.co/edit/7z5pU2bzFj3sA04ndsCa?p=preview For example the Menu Entries only change to the last parent state. You can see that the ionicView.enter will call on the wrong scope.
Will be resolved with v2 navigation
a bit too late for me, pity
Sounds like this might be the same issue as #2617 . Kind of silly that we can't really use one of the main features of ui-router (nested states), and haven't been able to for over a year now.
This is really important for me to cancel a timer and $ionicView.leave still not work
@onigetoc you can try setting view caching to false and using the $destroy event
I used the $destroy and it's working, but i do not understand why the IONIC theam do not fix the .leave function.
Any progress on this issue ?
I'm willing to beta test this issue if helpfull.
Hi Guys!
I was looking for the solution of my problem and I ended up here.
Could you guys give me some help?
If there is not the place to ask it, I ask for sorry.
I need that "$ionicView.afterEnter" be triggered in a view, but there's no way to get it working.
I don't understand what is going on.
The basic structure in my project is a login and after this a main page with a side menu.
When a go to the "app.newEvent" page to add itens to database, I'd like to clear the form before input some data, because maybe it was filled by user in a first time.
Below there's my $stateProvider:
(function() {
'use strict';
angular.module('app')
.config(appConfig);
appConfig.$inject = ['$stateProvider', '$urlRouterProvider', '$ionicConfigProvider'];
function appConfig($stateProvider, $urlRouterProvider, $ionicConfigProvider) {
$ionicConfigProvider.backButton.text('').icon('ion-ios-arrow-back')
$ionicConfigProvider.views.transition('ios');
/* $ionicConfigProvider.views.transition('none'); */
$stateProvider
.state('splash', {
url: '/splash',
templateUrl: 'templates/splash.html',
controller: 'ctrlSplash',
controllerAs: 'vm'
})
.state('login', {
url: '/login',
templateUrl: 'templates/login/login.html',
controller: 'ctrlLogin',
controllerAs: 'vm'
})
.state('signin', {
url: '/signin',
templateUrl: 'templates/login/signin.html',
controller: 'ctrlSignin',
controllerAs: 'vm'
})
.state('terms', {
url: '/terms',
templateUrl: 'templates/terms/terms.html',
})
.state('app', {
url: '/app',
abstract: true,
templateUrl: 'templates/menu/menu.html',
controller: 'ctrlMenu',
controllerAs: 'vm'
})
.state('app.main', {
url: '/main',
views: {
'menuContent': {
templateUrl: 'templates/main/main.html',
controller: 'ctrlMain',
controllerAs: 'vm'
},
'menuLeft': {
templateUrl: 'templates/menu/menuLeft.html',
controller: 'ctrlMenu',
controllerAs: 'vm'
}
}
})
.state('app.terms', {
url: '/terms',
views: {
'menuContent': {
templateUrl: 'templates/terms/terms.html'
},
'menuLeft': {
templateUrl: 'templates/menu/menuLeft.html',
controller: 'ctrlMenu',
controllerAs: 'vm'
}
}
})
.state('app.newEvent', {
url: '/newEvent',
cache: true,
views: {
'menuContent': {
templateUrl: 'templates/newEvent/newEvent.html',
controller: 'ctrlNewEvent',
controllerAs: 'vm'
},
'menuLeft': {
templateUrl: 'templates/menu/menuLeft.html',
controller: 'ctrlMenu',
controllerAs: 'vm'
}
}
})
/* this is a tab abstract wrapper */
.state('app.eventDetail', {
url: '/eventDetail',
abstract: true,
cache: true,
views: {
'menuContent': {
templateUrl: 'templates/eventDetail/eventDetail.html'
}
}
})
.state('app.eventDetail.poll', {
url: '/poll',
views: {
'tab-poll': {
templateUrl: 'templates/eventDetail/poll/poll.html',
controller: 'ctrlPoll as vm'
}
}
})
.state('app.eventDetail.participants', {
url: '/participants',
views: {
'tab-participants': {
templateUrl: 'templates/eventDetail/participants/participants.html',
controller: 'ctrlParticipants as vm'
}
}
})
.state('app.addPeople', {
url: '/addPeople',
views: {
'menuContent': {
templateUrl: 'templates/eventDetail/participants/addPeople/addPeople.html',
controller: 'ctrlAddPeople as vm'
}
}
})
.state('app.addPlace', {
url: '/addPlace',
views: {
'menuContent': {
templateUrl: 'templates/eventDetail/poll/addPlace/addPlace.html',
controller: 'ctrlAddPlace as vm'
}
}
})
// if none of the above states are matched, use this as the fallback
$urlRouterProvider.otherwise('/splash');
}
})();
This is the controller "ctrlNewEvent":
(function() {
'use strict';
angular.module('app')
.controller('ctrlNewEvent', ctrlNewEvent);
ctrlNewEvent.$inject = ['$scope', '$state', '$stateParams', '$location', '$ionicHistory', '$ionicPlatform', '$window', '$ionicScrollDelegate', '$ionicLoading', 'authFactory', 'eventsFactory', 'errMsgFactory'];
function ctrlNewEvent($scope, $state, $stateParams, $location, $ionicHistory, $ionicPlatform, $window, $ionicScrollDelegate, $ionicLoading, authFactory, eventsFactory, errMsgFactory) {
/* jshint validthis: true */
var vm = this;
vm.msgs = [];
vm.frmNew = {};
/* External Functions
----------------------------------------------------------------------*/
vm.goHome = goHome;
vm.createGroup = createGroup;
vm.dateClick = dateClick;
/* VARs - END -
===================================================================== */
vm.backButtonPressed = $ionicPlatform.registerBackButtonAction(
goHome, 1000
);
debug('ctrlNewEvent called! 1');
clearForm();
setFocus();
/* verify if user is logged
---------------------------------------------*/
$scope.$on('$ionicNavView.afterEnter', function() {
debug('$ionicNavView.afterEnter called');
});
$scope.$on("$ionicView.enter", function() {
debug("$ionicView.enter - NewEvent.controller.js");
});
$scope.$on('$ionicNavView.afterEnter', function() {
debug('ctrlNewEvent called! 2');
if ((authFactory.isLogged())) {
debug('ctrlNewEvent.isLogged() called - user logged');
} else {
debug('ctrlNewEvent.isLogged() called - user not logged');
authFactory.resetAuthUser();
$ionicHistory.nextViewOptions({
disableBack: true
});
$state.go('login');
}
});
$scope.$watch("vm.frmNew.groupName", function(newValue, oldValue) {
//debug('groupName: oldValue = ' + oldValue + ' / newValue = ' + newValue);
if ((vm.frmNew.groupName) && (vm.frmNew.groupName.length > 0) && (vm.frmNew.date)) {
vm.formOk = true;
} else {
vm.formOk = false;
};
});
$scope.$watch("vm.frmNew.date", function(newValue, oldValue) {
//debug('vm.frmNew.date: oldValue = ' + oldValue + ' / newValue = ' + newValue);
if ((vm.frmNew.groupName) && (vm.frmNew.groupName.length > 0) && (vm.frmNew.date)) {
vm.formOk = true;
} else {
vm.formOk = false;
};
});
/*--------------------------------------------------------------------
* Local functions
----------------------------------------------------------------------*/
function clearForm() {
vm.frmNew.groupName = '';
vm.frmNew.groupDesc = '';
vm.frmNew.date = new Date();
vm.frmNew.date.setMilliseconds(0);
vm.frmNew.date.setSeconds(0);
vm.formOk = false;
};
function gotoAfterCreate(objId) {
debug(objId);
eventsFactory.setActiveEvent(objId);
$ionicHistory.nextViewOptions({
disableBack: true
});
$state.go('app.eventDetail.participants');
};
function setFocus() {
debug('ctrlNewEvent.setFocus() called!')
$('#newgroupname').focus();
$location.hash('newgroupname');
$ionicScrollDelegate.anchorScroll();
};
function setLabels(date) {
vm.frmNew.dateLabel = getDateFormated(date);
vm.frmNew.timeLabel = getTimeFormated(date);
//vm.frmNew.dateTimeLabel = fomatDateTimeLabel(date);
};
function getDateFormated(inputDate) {
var today = inputDate;
if (typeof(today) === 'undefined') {
today = new Date();
}
var dd = today.getDate();
var mm = today.getMonth() + 1; //January is 0!
var yyyy = today.getFullYear();
if (dd < 10) {
dd = '0' + dd
}
if (mm < 10) {
mm = '0' + mm
}
//today = mm + '/' + dd + '/' + yyyy; <- American format
today = dd + '/' + mm + '/' + yyyy;
return today;
}
function getTimeFormated(inputTime) {
var iTime = inputTime;
if (typeof(iTime) === 'undefined') {
iTime = new Date();
iTime = new Date(iTime.getTime() - iTime.getTimezoneOffset());
}
var hh = iTime.getHours();
var mm = iTime.getMinutes();
if (hh < 10) {
hh = '0' + hh
}
if (mm < 10) {
mm = '0' + mm
}
var now = hh + ':' + mm;
return now;
}
/*--------------------------------------------------------------------
* Public functions
----------------------------------------------------------------------*/
function dateClick() {
debug('dateClick called!');
//angular.element('#datetimelocal').trigger('focus');
angular.element('#datetimelocal').trigger('click');
}
function createGroup() {
vm.msgs = [];
debug('newEvent.controller.js - createGroup() called');
$ionicLoading.show({
template: 'Criando evento...'
});
var nEvent = {};
nEvent.groupName = vm.frmNew.groupName;
nEvent.groupDesc = vm.frmNew.groupDesc;
nEvent.groupDate = vm.frmNew.date;
nEvent.groupTime = vm.frmNew.date;
/*
ver esse para usar:
personService.getPerson()
.then(function(personResult){
return TranslateData.getTranslation();
})
.then(function(translationResult){
// do something after translation
});
fonte: http://stackoverflow.com/questions/27603673/how-to-call-angular-factory-after-another-is-finished
*/
//alert('peopleSearch ' + vm.partialId)
eventsFactory.createEvent(nEvent).then(function(response) {
debug(response);
debug('createEvent().response[0].status = ' + response.status)
if (response.status == 0) {
debug('startVote() input parameters:');
debug(response);
debug(nEvent.groupDate);
debug(nEvent.groupTime);
eventsFactory.getAllEvents().then(function(resp2) {
debug('eventsFactory.getAllEvents():')
debug(resp2);
vm.groups = resp2;
}, function(error2) {
debug('eventsFactory.getAllEvents():')
debug(error2);
vm.msgs.push(error2.msg);
}).finally(function() {
gotoAfterCreate(response.data.groupid);
});
}
}, function(error) {
debug('eventsFactory.getAllEvents():')
debug(error);
vm.msgs.push(error.msg);
}).finally(function() {
$ionicLoading.hide();
});;
};
function goHome() {
$ionicHistory.nextViewOptions({
disableBack: true
});
$state.go('app.main');
};
}
})();
Thank you!