gpt4 book ai didi

angularjs - Controller 被启动两次

转载 作者:行者123 更新时间:2023-12-05 00:57:47 28 4
gpt4 key购买 nike

我有一个简单的 Controller (logincontroller),它在加载 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>

登录.html:
<div class="container centeredDiv" ng-controller="LoginController">
<p>Login page</p>
</div>

最佳答案

因为您的 Controller 在 2 个地方执行。

  • 来自 login.html - ng-controller
    <div class="container centeredDiv" ng-controller="LoginController">
    <p>Login page</p>
    </div>
  • 从与路由关联的 Controller -/Login
    .when('/Login', {
    controller: 'LoginController',
    templateUrl: 'Login.html'
    })

  • 怎么修?您应该删除 ng-controller来自 login.html 的属性

    关于angularjs - Controller 被启动两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34093163/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com