[英]Controller being called twice in Ionic (AngularJS)
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. 在我的角度项目中,用户接受EULA,然后自动重定向到他们的仪表板,但是,在此重定向上,DashboardController似乎被调用了两次,DashboardController正在路由本身被调用,我已经检查过我是否有意外在模板中再次调用它,但我没有。 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.
如果我直接或通过EULA控制器上的重定向访问URL似乎没有关系,我得到相同的结果。
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 根据评论添加我的HTML
index.html 的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 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>
If you are using ui-router
you don't have to use ng-controller
. 如果您使用的是
ui-router
,则不必使用ng-controller
。 You have used it in your dashboard.html , another is generated by ui-router
- that's why it is hit twice. 您已经在dashboard.html中使用了它,另一个是由
ui-router
生成的 - 这就是它被击中两次的原因。
好吧,经过长时间的调试和检查后,我发现这是一个与离线导航栏有关的问题,本质上,我正在调用<ui-view></ui-view>
& <ion-nav-view></ion-nav-view>
在同一页面上<ion-nav-view></ion-nav-view>
,因此基本上将我的视图加倍,而后者又调用了两次控制器。
I know this has been answered already as well, but I wanted to add my fix for the exact same problem. 我知道这已经得到了回答,但我想为完全相同的问题添加我的修复程序。
My controllers were also being called twice, but in my case I had to comment out the ng-controller
settings in various files: 我的控制器也被调用两次,但在我的情况下,我必须在各种文件中注释掉
ng-controller
设置:
My config function in the main app.js 我的主要app.js中的配置功能
.config(function($stateProvider, $urlRouterProvider) {
$stateProvider
.state('splash', {
url: "/",
templateUrl: "app/splash/splash.html"
// controller: 'SplashCtrl'
})
Since I was already calling it in the markup: 因为我已经在标记中调用它:
<ion-view view-title="TickerTags" ng-controller="SplashCtrl as splash">
<ion-content class="splash">
The controller key inside of my Directives 我的指令中的控制器密钥
angular
.module('tagsPanelDirective', [])
.controller('TagsPanelCtrl', TagsPanelCtrl)
.directive('tagsPanel', tagsPanel);
function tagsPanel() {
var directive = {
templateUrl: "app/tags/tagsPanel.html",
restrict: "E",
replace: true,
bindToController: true,
// controller: 'TagsPanelCtrl as tagsPanel',
link: link,
scope: false
};
return directive;
function link(scope, element, attrs) {}
}
Again since I was already calling it from within the template markup: 因为我已经从模板标记中调用它了:
<section class="tags-panel" ng-controller="TagsPanelCtrl as tagsPanel">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.