gpt4 book ai didi

javascript - ionic 框架 ionicView.entered 事件触发了两次

转载 作者:数据小太阳 更新时间:2023-10-29 05:47:57 28 4
gpt4 key购买 nike

我正在玩这个 ionicView 事件,只要 View 变为事件状态就会触发,我正在使用侧边菜单模板,您可以在创建项目时重复使用该模板。似乎如果我在 AppCtrl 中为 $ionicView.entered 事件设置一个监听器(侧面菜单模板使用的那个,属于 ui-router 配置中的抽象状态)它会连续调用两次 subview (例如,当使用 app.someotherview 作为状态时)。

我不知道这是否是预期的行为,因为从文档中我预计它只会触发一次,无论我是否更改 subview (menuContent View )。

我想知道这是否是预期的行为,如果是,我如何才能让事件在每次显示侧边菜单模板时只触发一次。

这是我写的:

这是来自应用程序模块:

.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 the above states are matched, use this as the fallback
$urlRouterProvider.otherwise('/login');

然后AppCtrl是这样的:

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 is fired twice in a row
console.log("App view (menu) entered.");
console.log(arguments);
});

$scope.$on('$ionicView.leave', function(){ //This just one when leaving, which happens when I logout
console.log("App view (menu) leaved.");
console.log(arguments);
});
});

菜单模板:

<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>

最佳答案

一个简单的解决方法是:

$scope.$on('$ionicView.enter', function(ev) {
if(ev.targetScope !== $scope)
return;
// Your code which should only run once
});

关于javascript - ionic 框架 ionicView.entered 事件触发了两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30236425/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com