javascript - Ionic framework ionicView.entered event fired twice -
i'm playing ionicview events fired whenever views become active, , i'm using side menu template can re-use when creating project. seems if put listener $ionicview.entered event in appctrl (the 1 used side menu template, belongs abstract state in ui-router configuration) gets called twice in row of subviews (like, when using app.someotherview state).
i don't know if expected behavior because documentation expect fire once, no matter if change subview (the menucontent view).
i'd know if expected behavior , if so, how have event fired once every time gets show side menu template.
this i've got written:
this application module:
.config(function($stateprovider, $urlrouterprovider, $httpprovider) { $stateprovider .state('app', { url: "/app", abstract: true, templateurl: "templates/menu.html", controller: 'appctrl' }) .state('app.overview', { url: "/overview", views: { 'menucontent': { templateurl: "templates/overview.html", controller: 'overviewctrl' } } }) .state('login', { url: "/login", templateurl: "templates/identificationscreen.html", controller: "identificationctrl" }) .state('app.agenda', { url: "/agenda", views: { 'menucontent': { templateurl: "templates/agenda.html", controller: 'agendactrl' } } }); $httpprovider.interceptors.push('credentialsinjector'); // if none of above states matched, use fallback $urlrouterprovider.otherwise('/login'); then appctrl this:
angular.module('dashboard.controllers.app', []) .controller('appctrl', function($scope, $ionicmodal, $timeout, $ionicsidemenudelegate, authservice, $state) { $scope.logout = function() { authservice.logout(); $state.go('login'); }; $scope.$on('$ionicview.enter', function(){ //this fired twice in row console.log("app view (menu) entered."); console.log(arguments); }); $scope.$on('$ionicview.leave', function(){ //this 1 when leaving, happens when logout console.log("app view (menu) leaved."); console.log(arguments); }); }); the menu template:
<ion-side-menus enable-menu-with-back-views="false"> <ion-side-menu-content edge-drag-threshold="true"> <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="menucontent"></ion-nav-view> </ion-side-menu-content> <ion-side-menu side="left"> <ion-header-bar class="bar-stable"> <h1 class="title">appoint!</h1> </ion-header-bar> <ion-content> <ion-list> <ion-item nav-clear menu-close href="#/app/overview"> overview </ion-item> <ion-item nav-clear menu-close href="#/app/agenda"> agenda </ion-item> <ion-item nav-clear menu-close ng-click="logout()"> logout </ion-item> </ion-list> </ion-content> </ion-side-menu> </ion-side-menus>
a simple workaround is:
$scope.$on('$ionicview.enter', function(ev) { if(ev.targetscope !== $scope) return; // code should run once });
Comments
Post a Comment