角度路由:当我调用控制器时,第二个将被执行

时间:2017-04-07 11:37:24

标签: angularjs

我正在使用角度来进行页面登录,我使用模板login.html和控制器login.html,问题是当我调用登录控制器时,主控制器将自动执行!

我发现:警告:尝试不止一次加载角度。

这是我的JS:



trainingApp.config(['$routeProvider' ,function($routeProvider){
    $routeProvider
    .when('/login',{
        templateUrl: 'login.html',
        controller:  'loginCtrl'
    })
    .when('/',{
        templateUrl: 'index.html',
        controller: 'mainCtrl'
    })
    .otherwise({
            redirectTo : "/"
        });
}]);



trainingApp.controller('loginCtrl',['$scope', function($scope){
$scope.alertt = function(){
   // console.log("ok");
    alert("okkkkkkk");
}
}]);




trainingApp.controller('mainCtrl', ['$scope','$timeout', 'apiService','$location', function ($scope,$timeout, apiService,$location) {
    console.log('hello');
    }]);




的index.html



<!DOCTYPE html>
<html lang="fr"> 
<head>
    <meta charset="UTF-8">
    <title>Plannification Des Formations</title> 
    <link rel="stylesheet" type="text/css" href="mystyle/bootstrap-tokenfield.css">
    
    <link rel="stylesheet" type="text/css" href="mystyle/bootstrap.min.css">
    <link rel="stylesheet" href="mystyle/bootstrap-datepicker.css">
    <link rel="stylesheet" href="mystyle/jquery-ui.css">
            <script src="public/jquery-1.11.2.min.js"></script> 
            <script src="public/jquery-ui.js"></script>
            <script src="public/bootstrap-datepicker.js"></script>
            <script src="node_modules/lodash/lodash.js"></script>
            <script src="bower_components/angular/angular.min.js"></script>
            <script src="bower_components/angular-route/angular-route.min.js"></script>
            <script src="bower_components/moment/min/moment-with-locales.min.js"></script>
            <script src="public/daypilot/daypilot-all.min.js"></script>
            <script src="public/bootstrap-tokenfield.js"></script>
            <script src="public/bootstrap.min.js"></script>
            <!-- <script src="bower_components/angular-route/angular-route.js"></script> -->
                  <script src="controllers/app.js"></script>
                     <script src="controllers/mainCrtl.js"></script>
                        <script src="services/apiService.js"></script>
                      
</head>
<body  data-ng-app="training" data-ng-controller="mainCtrl" ng-view>
.......
</body>
</html>
&#13;
&#13;
&#13;

的login.html

&#13;
&#13;
<button id="btn-login"  class="btn btn-success "  ng-href="#!/">Login  </button>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:2)

请从ng-controller="mainCtrl"删除html,因为您已在$routeProvider配置中定义了控制器。

答案 1 :(得分:0)

请更改您的代码。

.when('/',{
    templateUrl: 'index.html',
    controller: 'mainCtrl',
    abstract: true,
})