当页面加载时,activateController被调用两次

时间:2015-08-17 14:39:33

标签: javascript angularjs hottowel

我正在使用HotTowel.Angular,并注意到我的所有控制器都加载了两次。

enter image description here

这是dashbhoard.js

(function () {
    'use strict';
    var controllerId = 'dashboard';
    angular.module('app').controller(controllerId, ['common', 'datacontext', 'localStorageService', 'authenticator', dashboard]);

    function dashboard(common, datacontext, localStorageService, authenticator) {
        var getLogFn = common.logger.getLogFn;
        var log = getLogFn(controllerId);

        var vm = this;
        vm.title = 'Dashboard';
        vm.assignedToDoItems = [];
        vm.toogleAssignedToDoItem = toogleAssignedToDoItem;

        activate();

        function activate() {
            var promises = [getActions()];
            common.activateController(promises, controllerId)
                .then(function () { log('Activated Dashboard View'); });
        }

        function getActions() {
            return datacontext.assignedtodoitem.getAssignedToMe(authenticator.authData).then(function (data) {
                vm.assignedToDoItems = data;
                return vm.assignedToDoItems;
            });
        }

        function toogleAssignedToDoItem(todoItem) {
            todoItem.isCompleted = !todoItem.isCompleted;
            datacontext.save(todoItem);
        }
    }
})();

这是app.js

(function () {
    'use strict';

    var app = angular.module('app', [
        // Angular modules 
        'ngAnimate',        // animations
        'ngRoute',          // routing
        'ngSanitize',       // sanitizes html bindings (ex: sidebar.js)

        // Custom modules 
        'common',           // common functions, logger, spinner
        'common.bootstrap', // bootstrap dialog wrapper functions

        // 3rd Party Modules
        'ui.bootstrap',      // ui-bootstrap (ex: carousel, pagination, dialog)

        'ngzWip',

        //Breeze
        'breeze.angular',      
        'LocalStorageModule',

        'xeditable',
        'angularMoment',
        'mgo-angular-wizard'
    ]);

    app.config(['$httpProvider', function ($httpProvider) {
        $httpProvider.interceptors.push('authInterceptor');
    }]);

    // Handle routing errors and success events
    app.run(['$route', '$rootScope', '$q', 'breeze', 'datacontext', 'routemediator', 'editableOptions',
        function ($route, $rootScope, $q, breeze, datacontext, routemediator, editableOptions) {
        // Include $route to kick start the router.
        routemediator.setRoutingHandlers();
        editableOptions.theme = 'bs3';
        var ajaxAdapter = breeze.config.getAdapterInstance("ajax");
        breeze.ajaxpost(ajaxAdapter);
        }]);        
})();

这些是加载的所有脚本文件:

<!-- start: JavaScript-->
    <!--[if !IE]>-->
    <script src="Scripts/jquery-2.1.0.min.js"></script>
    <!--<![endif]-->
    <!--[if IE]>

        <script src="Scripts/jquery-1.11.0.min.js"></script>

    <![endif]-->
    <!--[if !IE]>-->
    <script type="text/javascript">
        window.jQuery || document.write("<script src='Scripts/jquery-2.1.0.min.js'>" + "<" + "/script>");
    </script>
    <!--<![endif]-->
    <!--[if IE]>

        <script type="text/javascript">
        window.jQuery || document.write("<script src='Scripts/jquery-1.11.0.min.js'>"+"<"+"/script>");
        </script>

    <![endif]-->
    <script src="Scripts/jquery-migrate-1.2.1.min.js"></script>
    <script src="Scripts/underscore.min.js"></script>

    <!-- page scripts -->
    <script src="Scripts/jquery-ui-1.10.3.custom.min.js"></script>
    <script src="Scripts/jquery.ui.touch-punch.min.js"></script>
    <script src="Scripts/jquery.sparkline.min.js"></script>
    <script src="Scripts/fullcalendar.min.js"></script>
    <!--[if lte IE 8]><script language="javascript" type="text/javascript" src="Scripts/excanvas.min.js"></script><![endif]-->
    <script src="Scripts/jquery.flot.min.js"></script>
    <script src="Scripts/jquery.flot.pie.min.js"></script>
    <script src="Scripts/jquery.flot.stack.min.js"></script>
    <script src="Scripts/jquery.flot.resize.min.js"></script>
    <script src="Scripts/jquery.flot.time.min.js"></script>
    <script src="Scripts/jquery.autosize.min.js"></script>
    <script src="Scripts/jquery.placeholder.min.js"></script>
    <script src="Scripts/wizard.min.js"></script>

    <!-- theme scripts -->
    <script src="Scripts/custom.min.js"></script>
    <script src="Scripts/core.min.js"></script>

    <!-- end: JavaScript-->

    <!-- Vendor Scripts -->    
    <script src="scripts/angular.js"></script>
    <script src="scripts/angular-route.js"></script>
    <script src="scripts/angular-sanitize.js"></script>
    <script src="Scripts/angular-local-storage.js"></script>
    <script src="Scripts/angular-animate.min.js"></script>
    <script src="scripts/bootstrap.min.js"></script>
    <script src="scripts/toastr.js"></script>
    <script src="scripts/moment.js"></script>
    <script src="//cdnjs.cloudflare.com/ajax/libs/angular-moment/0.9.0/angular-moment.min.js"></script>
    <script src="scripts/ui-bootstrap-tpls-0.10.0.js"></script>
    <script src="scripts/spin.js"></script>
    <script src="scripts/breeze.debug.js"></script>
    <script src="scripts/breeze.angular.js"></script>
    <script src="scripts/breeze.directives.js"></script>
    <script src="scripts/breeze.saveErrorExtensions.js"></script>
    <script src="Scripts/angular.breeze.storagewip.js"></script>
    <script src="Scripts/breeze.ajaxpost.js"></script>
    <script src="Scripts/angular-wizard.min.js"></script>

    <!-- Needed only if you are using to$q -->
    <script src="scripts/breeze.to$q.shim.js"></script> 

    <!-- Bootstrapping -->
    <script src="app/app.js"></script>
    <script src="app/config.js"></script>
    <script src="app/config.exceptionHandler.js"></script>
    <script src="app/config.route.js"></script>

    <!-- common Modules -->
    <script src="app/common/common.js"></script>
    <script src="app/common/logger.js"></script>
    <script src="app/common/spinner.js"></script>
    <script src="Scripts/angular-xeditable-0.1.8/js/xeditable.min.js"></script>

    <!-- common.bootstrap Modules -->
    <script src="app/common/bootstrap/bootstrap.dialog.js"></script>

    <!-- app -->
    <script src="app/dashboard/dashboard.js"></script>
    <script src="app/layout/shell.js"></script>
    <script src="app/layout/sidebar.js"></script>
    <script src="app/discussions/discussions.js"></script>
    <script src="app/discussion/discussion.js"></script>
    <script src="app/goals/goals.js"></script>
    <script src="app/goal/goal.js"></script>
    <script src="app/goal/addMultipleItemsModalInstanceCtrl.js"></script>
    <script src="app/goal/addContributorsModalInstanceCtrl.js"></script>
    <script src="app/goal/assignEvaluationContributorsModalInstanceCtrl.js"></script>
    <script src="app/goal/assignToDoItemContributorsModalInstanceCtrl.js"></script>
    <script src="app/evaluations/startEvaluationModalCtrl.js"></script>
    <script src="app/evaluations/evaluations.js"></script>
    <script src="app/teams/teams.js"></script>
    <script src="app/login/login.js"></script>
    <script src="app/signup/signup.js"></script>
    <script src="app/invite/invite.js"></script>
    <script src="app/profile/profile.js"></script>
    <script src="app/settings/settings.js"></script>

    <!-- app Services -->
    <script src="app/services/authenticator.js"></script>
    <script src="app/services/authInterceptor.js"></script>
    <script src="app/services/routemediator.js"></script>
    <script src="app/services/datacontext.js"></script>
    <script src="app/services/directives.js"></script>
    <script src="app/services/entityManagerFactory.js"></script>
    <script src="app/services/model.js"></script>    
    <script src="app/services/repositories.js"></script>
    <script src="app/services/repository.abstract.js"></script>
    <script src="app/services/repository.evaluation.js"></script>
    <script src="app/services/repository.discussion.js"></script>
    <script src="app/services/repository.comment.js"></script>
    <script src="app/services/repository.todoitem.js"></script>
    <script src="app/services/repository.goal.js"></script>    
    <script src="app/services/repository.lookup.js"></script>
    <script src="app/services/repository.team.js"></script>
    <script src="app/services/repository.teammember.js"></script>
    <script src="app/services/repository.contributor.js"></script>
    <script src="app/services/repository.evaluationcontributor.js"></script>
    <script src="app/services/repository.todoitemcontributor.js"></script>
    <script src="app/services/repository.assignedtodoitem.js"></script>
    <script src="app/services/repository.aspnetuser.js"></script>
    <script src="app/services/repository.assignedevaluation.js"></script>
    <script src="app/services/repository.dowell.js"></script>
    <script src="app/services/repository.didnotdowell.js"></script>

我使用以下版本:

  • AngularJS v1.2.22
  • Breeze.Angular.js v.0.9.0

---- ---- EDIT config.route.js

(function () {
    'use strict';

    var app = angular.module('app');

    // Collect the routes
    app.constant('routes', getRoutes());

    // Configure the routes and route resolvers
    app.config(['$routeProvider', 'routes', routeConfigurator]);
    function routeConfigurator($routeProvider, routes) {     

        routes.forEach(function (r) {
            //$routeProvider.when(r.url, r.config);
            setRoute(r.url, r.config);
        });
        $routeProvider.otherwise({ redirectTo: '/' });

        function setRoute(url, definition) {
            definition.resolve = angular.extend(definition.resolve || {}, {
                prime: prime,
                checkSecurity: checkSecurity
            });
            $routeProvider.when(url, definition);
        }
    }

    prime.$inject = ['datacontext'];
    function prime(dc) {
        return dc.prime();
    }

    checkSecurity.$inject = ['$route', 'authenticator', 'common'];
    function checkSecurity($route, authenticator, common) {
        var deferred = common.$q.defer();
        authenticator.fillData().then(function () {
            var settings = $route.current.settings;
            var loginRequired = settings.loginRequired || false;
            var roles = settings.roles || [];
            if (loginRequired) {
                if (!authenticator.authData.isAuth) {
                    common.$location.path('/login');
                } else {
                    if (roles.length > 0) {
                        if (!common.checkRole(authenticator.authData.roles, roles)) {
                            common.$location.path('/notauthorized').replace();
                        }
                    }
                }
            }
            deferred.resolve(true); //We want to return just true even if we have to re-route. 
            //If we returned an reject, the the global handler will re-route us to home
        }, function (error) {
            deferred.reject

(错误);         });

    return deferred.promise;
}

// Define the routes 
function getRoutes() {
    return [
        {
            url: '/',
            config: {
                templateUrl: 'app/dashboard/dashboard.html',
                title: 'Dashboard',
                settings: {
                    nav: 1,
                    loginRequired: true,
                    roles: [],
                    content: '<i class="fa fa-dashboard"></i> Dashboard'
                }
            }
        }, {

            url: '/goals',
            config: {
                title: 'Goals',
                templateUrl: 'app/goals/goals.html',
                settings: {
                    nav: 2,
                    loginRequired: true,
                    roles: [],
                    content: '<i class="fa fa-bullseye"></i> Goals'
                }
            }
        }, {

            url: '/goal/:id',
            config: {
                title: 'Goals',
                templateUrl: 'app/goal/goal.html',
                settings: {
                    loginRequired: true,
                    roles: []
                }
            }
        }, {

            url: '/goals/search/:search',
            config: {
                title: 'goals-search',
                templateUrl: 'app/goals/goals.html',
                settings: {
                    loginRequired: true,
                    roles: []
                }
            }
        }, {

            url: '/evaluations',
            config: {
                title: 'Evaluations',
                templateUrl: 'app/evaluations/evaluations.html',
                settings: {
                    nav: 3,
                    loginRequired: true,
                    roles: [],
                    content: '<i class="fa fa-book"></i> Evaluations'
                }
            }
        }, {
            url: '/profile',
            config: {
                title: 'profiles',
                templateUrl: 'app/profile/profile.html',
                settings: {
                    loginRequired: true,
                    roles: []
                }
            }
        }, {
            url: '/settings',
            config: {
                title: 'settings',
                templateUrl: 'app/settings/settings.html',
                settings: {
                    loginRequired: true,
                    roles: []
                }
            }
        }, {
            url: '/login',
            config: {
                title: 'login',
                templateUrl: 'app/login/login.html',
                settings: {
                }
            }
        }, {
            url: '/signup',
            config: {
                title: 'Sign Up',
                templateUrl: 'app/signup/signup.html',
                settings: {
                }
            }
        }, {
            url: '/deactivated',
            config: {
                title: 'Oh Man!!',
                templateUrl: 'app/deactivated/deactivated.html',
                settings: {
                }
            }
        },{
            url: '/notauthorized',
            config: {
                title: 'not authorized',
                templateUrl: 'app/home/notauthorized.html',
                settings: {
                }
            }
        }
    ];
}

})();

1 个答案:

答案 0 :(得分:2)

你是否也在html中定义了控制器?如果你在路线和html中有它,那么它将被调用两次