控制器在离子被称为两次(AngularJS) [英] Controller being called twice in Ionic (AngularJS)

查看:229
本文介绍了控制器在离子被称为两次(AngularJS)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的角度项目的用户接受EULA随后会自动重定向到自己的仪表板,然而,在这个重定向DashboardController好像是被称为两次,DashboardController被称为路由本身,我已检查,看看是否我曾无意中再次调用它的模板,但我没有带。下面是我的路线和放大器;控制器。这似乎并不重要,如果我直接或通过协议控制器上的重定向访问URL,我得到了相同的结果。

的路由

 的.config(函数($ httpProvider,$ stateProvider,$ urlRouterProvider){    $ httpProvider.interceptors.push('HTT prequestInterceptor');    $ urlRouterProvider.otherwise('/');    $ stateProvider    .STATE('登陆',{
        网址:'/',
        templateUrl:'模板/ login.html的,
        数据:{
            requireLogin:假的
        }
    })
    .STATE('协议',{
        网址:'/ EULA,
        templateUrl:'模板/ eula.html',
        数据:{
            requireLogin:真
        }
    })
    .STATE(仪表盘,{
        网址:'/组',
        templateUrl:'模板/ dashboard.html',
        数据:{
            requireLogin:真
        }
    })
});

控制器:

  App.controller('DashboardController',['$范围,RequestService','$州','$ rootScope',函数($范围,RequestService,$状态$ rootScope){警报('测试');}]);

任何想法?

将我的HTML按照注释

的index.html

 <机身NG-应用=应用程序><离子导航栏类=酒吧阳性NAV-标题幻灯片ios7对齐标题=中心>
        <离子NAV-后退按钮类=按钮图标离子左箭头-C>< /离子NAV-后退按钮>
    < /离子导航栏>
    <离子导航视图类=滑 - 左 - 右>< /离子NAV-视图>
    < UI的视图>< / UI的视图>
< /身体GT;

dashboard.html

 < D​​IV CLASS =群体NG控制器=DashboardController>
    <离子视图标题=应用程序>        <离子NAV-按键侧=右>
            <一个UI的SREF =groupcreate><跨度类=图标离子IOS加纲要>< / SPAN>< / A>
        < /离子NAV-按钮>        <离子内容类=填充>
            < D​​IV CLASS =行>
                < D​​IV CLASS =COL-50NG-重复=成群组>
                    {{组}} 1
                < / DIV>
            < / DIV>
        < /离子含量>
    < /离子视图>
< / DIV>


解决方案

确定后很长一段时间的调试,以便检查的东西了,我发现,这是与导航栏离子,基本上是一个问题,我打电话< UI的视图>< / UI的视图> &安培; <离子NAV-视图>< /离子导航视图>在同一页面上,所以基本上我的看法这又在呼唤控制器增加了一倍两倍。

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.

这篇关于控制器在离子被称为两次(AngularJS)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆