控制器在Ionic中被调用两次(AngularJS)

时间:2015-06-01 08:43:46

标签: javascript angularjs ionic-framework angularjs-routing angularjs-controller

在我的角度项目中,用户接受EULA,然后自动重定向到他们的仪表板,但是,在此重定向上DashboardController似乎被调用了两次,DashboardController正在路由本身被调用,我已检查是否我在模板中意外地再次调用它,但我还没有。以下是我的路线&控制器。如果我直接访问URL或通过EULA控制器上的重定向,它似乎并不重要,我得到相同的结果。

路线

.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
        }
    })
});

控制器:

App.controller('DashboardController', ['$scope', 'RequestService', '$state', '$rootScope', function($scope, RequestService, $state, $rootScope){

alert('test');

}]);

有什么想法吗?

根据评论添加我的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

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

3 个答案:

答案 0 :(得分:8)

如果您使用ui-router,则不必使用ng-controller。您已经在 dashboard.html 中使用了它,另一个是由ui-router生成的 - 这就是它被击中两次的原因。

答案 1 :(得分:5)

好的,经过长时间的调试和检查后,我发现这是一个与离线导航栏有关的问题,基本上,我打电话给<ui-view></ui-view>&amp; <ion-nav-view></ion-nav-view>在同一页面上,所以基本上加倍了我的观点,而这反过来又召唤了两次控制器。

答案 2 :(得分:1)

我知道这已经得到了解答,但我想为完全相同的问题添加我的修复程序。

我的控制器也被调用了两次,但就我而言,我必须在各种文件中注释掉ng-controller设置:

主app.js中的我的配置功能

.config(function($stateProvider, $urlRouterProvider) {
    $stateProvider
        .state('splash', {
            url: "/",
            templateUrl: "app/splash/splash.html"
            // controller: 'SplashCtrl'
        })

因为我已在标记中调用它:

<ion-view view-title="TickerTags" ng-controller="SplashCtrl as splash">
    <ion-content class="splash">

我的指令中的控制器键

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) {}
}

再次,因为我已经在模板标记中调用它:

<section class="tags-panel" ng-controller="TagsPanelCtrl as tagsPanel">
相关问题