本文介绍了控制器在 Ionic (AngularJS) 中被调用两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的 angular 项目中,用户接受 EULA 然后自动重定向到他们的仪表板,但是,在此重定向中,DashboardController 似乎被调用了两次,DashboardController 正在路由本身上被调用,我已经检查过我不小心在模板中再次调用了它,但我没有.以下是我的路线&控制器.无论我是直接访问 URL 还是通过 EULA 控制器上的重定向访问 URL,我都会得到相同的结果.
路线
.config(function($httpProvider, $stateProvider, $urlRouterProvider) {$httpProvider.interceptors.push('httpRequestInterceptor');$urlRouterProvider.otherwise('/');$stateProvider.state('登录', {网址:'/',templateUrl: 'templates/login.html',数据: {要求登录:假}}).state('eula', {网址:'/eula',templateUrl: 'templates/eula.html',数据: {要求登录:true}}).state('仪表板', {网址:'/组',templateUrl: 'templates/dashboard.html',数据: {要求登录:true}})});
控制器:
App.controller('DashboardController', ['$scope', 'RequestService', '$state', '$rootScope', function($scope, RequestService, $state, $rootScope){警报('测试');}]);
有什么想法吗?
根据评论添加我的 HTML
index.html
dashboard.html
<ion-view title="应用程序"><ion-nav-buttons side="right"><a ui-sref="groupcreate"><span class="icon ion-ios-plus-outline"></span></a></ion-nav-buttons><ion-content class="padding"><div class="row"><div class="col-50" ng-repeat="group in groups">{{组}} 1
</离子含量></ion-view>
解决方案
好的,经过长时间的调试和检查,我发现这是一个与 ionic 中的导航栏有关的问题,本质上,我正在调用
&<ion-nav-view></ion-nav-view>
在同一页面上,所以基本上我的视图加倍,这反过来又调用了控制器两次.
In my angular project the user accepts a EULA then get automatically redirected to their dashboard, however, on this redirect the DashboardController seems to be being called twice, the DashboardController is being called on the route itself, I have checked to see if I have accidently called it again in the template but I havn't. Below is my route & controller. It doesn't appear to matter if I access the URL directly or via the redirect on the EULA controller, I get the same result.
The routes
.config(function($httpProvider, $stateProvider, $urlRouterProvider) {
$httpProvider.interceptors.push('httpRequestInterceptor');
$urlRouterProvider.otherwise('/');
$stateProvider
.state('login', {
url: '/',
templateUrl: 'templates/login.html',
data: {
requireLogin: false
}
})
.state('eula', {
url: '/eula',
templateUrl: 'templates/eula.html',
data: {
requireLogin: true
}
})
.state('dashboard', {
url: '/groups',
templateUrl: 'templates/dashboard.html',
data: {
requireLogin: true
}
})
});
The controller:
App.controller('DashboardController', ['$scope', 'RequestService', '$state', '$rootScope', function($scope, RequestService, $state, $rootScope){
alert('test');
}]);
Any ideas?
ADDED MY HTML AS PER COMMENTS
index.html
<body ng-app="App">
<ion-nav-bar class="bar-positive nav-title-slide-ios7" align-title="center">
<ion-nav-back-button class="button-icon ion-arrow-left-c"></ion-nav-back-button>
</ion-nav-bar>
<ion-nav-view class="slide-left-right"></ion-nav-view>
<ui-view></ui-view>
</body>
dashboard.html
<div class="groups" ng-controller="DashboardController">
<ion-view title="App">
<ion-nav-buttons side="right">
<a ui-sref="groupcreate"><span class="icon ion-ios-plus-outline"></span></a>
</ion-nav-buttons>
<ion-content class="padding">
<div class="row">
<div class="col-50" ng-repeat="group in groups">
{{ group }} 1
</div>
</div>
</ion-content>
</ion-view>
</div>
解决方案
Ok so after a long time debugging and check stuff out, I found out that it was an issue relating to the Nav Bar in ionic, essentially, I was calling <ui-view></ui-view>
& <ion-nav-view></ion-nav-view>
on the same page, so basically doubling up on my views which in turn was calling the controller twice.
这篇关于控制器在 Ionic (AngularJS) 中被调用两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!