2015-12-04 89 views
4

我有一個簡單的控制器(登錄控制器),它會在加載login.html時啓動兩次,但我不知道爲什麼會發生這種情況。控制器啓動兩次

MyApp.js:

angular.module('PVM', [ 
    'Authentication', 
    'Modal', 
    'ngRoute' 
]) 
.config([ 
    '$routeProvider', function($routeProvider) { 
     $routeProvider 
      .when('/Login', { 
       controller: 'LoginController', 
       templateUrl: 'Login.html' 
      }) 
      .when('/Home', { 
       controller: 'ModalController', 
       templateUrl: 'Home.html' 
      }) 
      .otherwise({ 
       redirectTo: '/Login' 
      }); 
    } 
]); 

LoginController.js:

angular.module("Authentication") 
.controller("LoginController", 
[ 
    "$scope", "$rootScope", "$location", "AuthenticationService", 
    function($scope, $rootScope, $location, AuthenticationService) { 
     AuthenticationService.ClearCredentials(); 

     $scope.login = function() { 

      var foo = "bar"; //Breakpoint hits here twice when loading login.html 
     }; 
    } 
    ]); 

的index.html:

<!DOCTYPE html> 
<html ng-app="PVM"> 
<head> 
<title>My Ang</title> 
<link href="Content/bootstrap.min.css" rel="stylesheet" /> 
<link href="Content/bootstrap-theme.min.css" rel="stylesheet" /> 
<link href="Content/MyCss.css" rel="stylesheet" /> 
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"> 

</head> 
<body> 
<div> 
<div ng-view></div> 
</div> 


<script src="//code.jquery.com/jquery-2.0.3.min.js"></script> 
<script src="Scripts/bootstrap.min.js"></script> 
<script src="http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.0-beta.2/angular.min.js"></script> 
<script src="http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.0-beta.2/angular-route.min.js"></script> 
<script src="http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.0-beta.2/angular-animate.min.js"></script> 
<script src="http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.0-beta.2/angular-cookies.min.js"></script> 
<script src="http://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/0.14.3/ui-bootstrap-tpls.min.js"></script> 


<script src="Scripts/Services/ModalService.js"></script> 
<script src="Scripts/Services/AuthenticationService.js"></script> 
<script src="Scripts/Controller/LoginController.js"></script> 
<script src="Scripts/Controller/ModalController.js"></script> 
<script src="Scripts/MyApp.js"></script> 


</body> 
</html> 

的login.html:

<div class="container centeredDiv" ng-controller="LoginController"> 
    <p>Login page</p> 
</div> 
+0

這是因爲你實例化控制器兩次:一次在你的'$ routeProvider'中,再次通過'ng-controller'在你的視圖中 –

回答

5

因爲你的控制器被在2個地方執行。

  1. 從login.html的 - NG-控制器

    <div class="container centeredDiv" ng-controller="LoginController"> 
        <p>Login page</p> 
    </div> 
    
  2. 從控制器路由關聯 - /登錄

    .when('/Login', { 
        controller: 'LoginController', 
        templateUrl: 'Login.html' 
    }) 
    

如何解決?您應該從login.html中刪除ng-controller屬性

+0

並且找到正確的方法來做到這一點? – MrProgram

+1

您應該從login.html中刪除ng-controller。看到我更新的答案。 – nikhil

0

您的模板login.html中不需要ng控制器,因爲它在您的路由中。簡單地刪除它,這應該做的伎倆。

+0

基本上nikhil已經說過了! – Mil4n

+0

你是對的。同樣的答案 – Maccurt