James Ives
James Ives

Reputation: 3365

Script firing twice in Main Controller

I'm attempting to implement KonamiJS within my AngularJS app that opens up a BootstrapUI modal dialog box when a user hits the Konami code on their keyboard. The script works, however it's firing twice and I can't seem to figure out why. This is what I have in my main controller that wraps all of the content on the page so it can be triggered from anywhere. Within the main controller I have an Angular view also:

<body ng-app="App" ng-controller="MainCtrl">
  <div id="main-content" ng-view="" class="content"></div>
</body>


angular.module('App')
  .controller('MainCtrl', function ($scope, $modal, $log) {

      $scope.animationsEnabled = true;
      var credits_page = new Konami();

      credits_page.code = function() {
         $modal.open({
           animation: $scope.animationsEnabled,
           templateUrl: 'views/credits.html',
           controller: 'ModalInstanceCtrl'
         });

         console.log("Trigger");
      }

      credits_page.load();
});

And then I have a modal controller that this talks to:

  angular.module('App')
    .controller('ModalInstanceCtrl', function ($scope, $modalInstance) {

      $scope.ok = function () {
            $modalInstance.close($scope.selected.item);
          };

      $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
          };
    });

And just incase here is my script load order:

    <script src="scripts/app.js"></script>
    <script src="scripts/controllers/main.js"></script>
    <script src="scripts/controllers/modal.js"></script>
    <script src="scripts/controllers/carousel.js"></script>
    <script src="scripts/controllers/contact.js"></script>
    <script src="scripts/particles.js"></script>
    <script src="scripts/konami.js"></script>

I assume it has something to do with the view switch but I can't put my finger on what exactly is causing it.

Upvotes: 1

Views: 149

Answers (2)

Kais
Kais

Reputation: 2008

check your template and route you may have declare controller twice

in template: ng-controller="MainCtrl"

in route: controller:'MainCtrl'

Upvotes: 0

Pankaj Parkar
Pankaj Parkar

Reputation: 136144

You must have mention MainCtrl inside your $routeProvider defination, so basically what is happening your MainCtrl controller is getting register twice.

You need to remove ng-controller="MainCtrl" from the body tag, which basically seems redundant as it instantiating MainCtrl twice.

Upvotes: 2

Related Questions