2016-12-16 21 views
1

私はこの例を使用して、同じページに、異なるテーマをルーティングするために自分のアプリケーションをngrouteモジュールを使用する:異なるこの例のルートで角度ngroute:コントローラのdoesntの仕事

<html> 
 
    
 
    <head> 
 
     <title>Angular JS Views</title> 
 
     <script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script> 
 
     <script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular-route.min.js"></script> 
 
    </head> 
 
    
 
    <body> 
 
     <h2>AngularJS Sample Application</h2> 
 
     <div ng-app = "mainApp"> 
 
     <p><a href = "#addStudent">Add Student</a></p> 
 
     <p><a href = "#viewStudents">View Students</a></p> 
 
     <div ng-view></div> 
 
      
 
     <script type = "text/ng-template" id = "addStudent.htm"> 
 
      <h2> Add Student </h2> 
 
      {{message}} 
 
     </script> 
 
      
 
     <script type = "text/ng-template" id = "viewStudents.htm"> 
 
      <h2> View Students </h2> 
 
      {{message}} 
 
     </script> 
 
     </div> 
 
     
 
     <script> 
 
     var mainApp = angular.module("mainApp", ['ngRoute']); 
 
     mainApp.config(['$routeProvider', function($routeProvider) { 
 
      $routeProvider. 
 
      
 
      when('/addStudent', { 
 
       templateUrl: 'addStudent.htm', 
 
       controller: 'AddStudentController' 
 
      }). 
 
      
 
      when('/viewStudents', { 
 
       templateUrl: 'viewStudents.htm', 
 
       controller: 'ViewStudentsController' 
 
      }). 
 
      
 
      otherwise({ 
 
       redirectTo: '/addStudent' 
 
      }); 
 
     }]); 
 
      
 
     mainApp.controller('AddStudentController', function($scope) { 
 
      console.log("adssadsd"); 
 
      $scope.message = "This page will be used to display add student form"; 
 
     }); 
 
      
 
     mainApp.controller('ViewStudentsController', function($scope) { 
 
      $scope.message = "This page will be used to display all the students"; 
 
     }); 
 
\t \t \t 
 
     </script> 
 
     
 
    </body> 
 
</html>

同じページ内のテーマ: enter image description here

enter image description here

このように。私のアプリケーションでそれをしよう: htmlファイル:私は私のコントローラを入れて、私のjsファイルで

//'use strict'; //questo mi da informazione sull'errore 
 

 
var app = angular.module("StaffManagement", ['ngRoute']); 
 

 
app.controller("LoginController", function($scope, $http, restService) { 
 
    $scope.stafflogins = []; 
 
    $scope.staffLoginForm = { 
 
     email: "", 
 
     pass: "" 
 
    }; 
 
    $scope.tokenStaffForm = { 
 
     idtokenstaff: -1,  
 
     tokenstaff: "" 
 
    }; 
 
    $scope.staffForm = { 
 
     idstaff : -1,  
 
     staffType: { 
 
      idstaffType: -1, 
 
      type: "" 
 
     }, 
 
     name: "", 
 
     surname: "", 
 
     birthDate: "", 
 
     phone: "", 
 
     gender: true, 
 
     working: true, 
 
     staffLogin: { 
 
      idstaffLogin: -1, 
 
      email: "", 
 
      pass: "" 
 
      } 
 
    }; 
 
    
 
    
 
    
 
    //8njae3j4b54fpoapftc8aofbfs 
 
    
 
    //admin: l5qsngh3v9a5f2v9p55ar4h083 
 
    $scope.submitCredentials= function() { 
 
     restService.login($scope.staffLoginForm, _logsuccess, _logerror); 
 
     
 
     //$location.url('edit_employee.html'); 
 
//  console.log($scope.staffLoginForm); 
 
     /* $http({ 
 
      method : 'POST', 
 
      url : 'http://localhost:8080/FoodDrinkDispener/rest/tokenstaff', 
 
      data : angular.toJson($scope.staffLoginForm), 
 
      }).then(function successCallback(response) { 
 
        if (typeof response.data === 'object'){ 
 
         _logsuccess(response) 
 
         return response.status; 
 
        } 
 
        else 
 
         _logerror(response); 
 
        },function (response) { 
 
        console.log($scope.tokenStaffForm); 
 
         _logerror(response); 
 
        } 
 
        );*/ 
 
      
 
       } 
 
    
 

 
       
 
    /*   
 
    function _SetToken(CurrentToken) { 
 
     sessionStorage.setItem("token", (CurrentToken === null) ? null : JSON.stringify(CurrentToken)); 
 
     //console.log(CurrentToken);  
 
    } 
 
     */ 
 
    function _logsuccess(response) { 
 
     console.log("Loggato correttamente"); 
 
     console.log(response.status); 
 
     var CurrentToken = response.data; 
 
     // _SetToken(CurrentToken);           
 
    } 
 
      
 
    function _logerror(response) { 
 
     console.log("Login fallito"); 
 
     console.log(response.status); 
 
     // _SetToken(null);          
 
    } 
 
     
 

 
     /* $http({ 
 
      method : 'GET', 
 
      url : 'http://localhost:8080/FoodDrinkDispener/rest/stafflogin/byemail/'+$scope.staffLoginForm.email 
 
      }).then(function successCallback(response) { 
 
     $scope.stafflogins = response.data; 
 
     }, function errorCallback(response) { 
 
     console.log(response.statusText); 
 
     }); 
 
     if(MD5($scope.stafflogins.pass)==$scope.staffLoginForm.pass)*/ 
 
      
 
    
 
    
 
    
 

 
    // console.log(sessionStorage.getItem.toString); 
 
    }); 
 

 
app.config(['$routeProvider', function($routeProvider) { 
 
      $routeProvider. 
 
      
 
      when('/sessionexpired', { 
 
       templateUrl: 'addStudent.htm', 
 
       controller: 'AddStudentController' 
 
      }). 
 
      
 
      when('/viewStudents', { 
 
       templateUrl: 'viewStudents.htm', 
 
       controller: 'ViewStudentsController' 
 
      }). 
 
      
 
      otherwise({ 
 
       redirectTo: '/addStudent' 
 
      }); 
 
     }]); 
 

 
app.controller('AddStudentController', function($scope) { 
 
     console.log("ciao"); 
 
      $scope.message = "This page will be used to display add student form"; 
 
     }); 
 
      
 
     app.controller('ViewStudentsController', function($scope) { 
 
       console.log("ciao"); 
 
      $scope.message = "This page will be used to display all the students"; 
 
     });

:ファイルを関連付けられている

<!DOCTYPE html> 
 
<!-- saved from url=(0066)https://hackerstribe.com/guide/IT-bootstrap-3.1.1/examples/signin/ --> 
 
<html lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
 
     <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.js"></script> 
 

 
    
 
     <script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular-route.min.js"></script> 
 
    
 
    <script type="application/javascript"></script> 
 
    <script src="scripts/login-controller.js"></script> 
 
    <script src="scripts/rest-services.js"></script> 
 
    <meta http-equiv="X-UA-Compatible" content="IE=edge"> 
 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
 
    <meta name="description" content=""> 
 
    <meta name="author" content=""> 
 
    <link rel="shortcut icon" href="images/puzzle.png" /> 
 

 
    
 

 

 
    
 
    <title>Login</title> 
 

 
    <!-- Bootstrap core CSS --> 
 
    <link href="./styles/bootstrap.min.css" rel="stylesheet"> 
 

 
    <!-- Custom styles for this template --> 
 
    <link href="./styles/signin.css" rel="stylesheet"> 
 

 
    <!-- Just for debugging purposes. Don't actually copy this line! --> 
 
    <!--[if lt IE 9]><script src="../../assets/js/ie8-responsive-file-warning.js"></script><![endif]--> 
 

 
    <!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries --> 
 
    <!--[if lt IE 9]> 
 
     <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script> 
 
     <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script> 
 
    <![endif]--> 
 
    </head> 
 

 
    <body ng-app="StaffManagement" ng-controller="LoginController" ng-fa> 
 

 
    <div class="container"> 
 
<script type = "text/ng-template" id = "addStudent.htm"> 
 
      <h2> Add Student </h2> 
 
      {{message}} 
 
     </script> 
 
      
 
     <script type = "text/ng-template" id = "viewStudents.htm"> 
 
      <h2> View Students </h2> 
 
      {{message}} 
 
     </script> 
 
     <form class="form-signin" role="form" ng-controller="LoginController" ng-submit="submitCredentials()" novalidate> 
 
     <h2 class="form-signin-heading">Hai bisogno di autenticarti</h2> 
 
     <input type="email" class="form-control" placeholder="Email address" required="" autofocus="" id="email" ng-model="staffLoginForm.email"> 
 
     <input type="password" class="form-control" placeholder="Password" required="" id="password" ng-model="staffLoginForm.pass"> 
 
     <label class="checkbox"> 
 
      <input type="checkbox" value="remember-me"> Ricordami 
 
     </label> 
 
     <button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button> 
 
     </form> 
 

 
    </div> <!-- /container --> 
 

 

 
    <!-- Bootstrap core JavaScript 
 
    ================================================== --> 
 
    <!-- Placed at the end of the document so the pages load faster --> 
 
    
 
     
 
    
 
     
 

 
</body></html>

JSをconfig:

app.config(['$routeProvider', function($routeProvider) { 
 
      $routeProvider. 
 
      
 
      when('/sessionexpired', { 
 
       templateUrl: 'addStudent.htm', 
 
       controller: 'AddStudentController' 
 
      }). 
 
      
 
      when('/viewStudents', { 
 
       templateUrl: 'viewStudents.htm', 
 
       controller: 'ViewStudentsController' 
 
      }). 
 
      
 
      otherwise({ 
 
       redirectTo: '/addStudent' 
 
      }); 
 
     }]); 
 

 
app.controller('AddStudentController', function($scope) { 
 
     console.log("ciao"); 
 
      $scope.message = "This page will be used to display add student form"; 
 
     }); 
 
      
 
     app.controller('ViewStudentsController', function($scope) { 
 
       console.log("ciao"); 
 
      $scope.message = "This page will be used to display all the students"; 
 
     });

私もconsole.log("ciao");にしようと試みたが、私はこのURLをしようとすると:ファイル:/// C:/ユーザー/ジャコモ%の20B /デスクトップ/ PROGETTO%20mainetti /証明%20angular/fddWebapp /login.html#/viewStudents何も表示されず、console.logも表示されません。 enter image description here

問題はどこですか?なぜ私のコントローラが動作しないのですか?

+0

'config'にルートがありません。デフォルトは/ addStudentに行きますが、addStudentルートはありません。 –

答えて

2

私はいくつかの問題を参照してください。あなたは、あなたのHTMLページに

  • <div ng-view></div>タグを持っていない(私は'/sessionexpired'があるべきと思いますが'/addStudent'にリダイレクトしているが、あなたはこのルートが設定されていない

    1. あなたはng-controller="LoginController"を2回宣言しました。最後のもので十分です。
  • +0

    いいえ、私は/sessionexpired..howを試しています。どのようにng-viewタグを設定できますか? –

    +0

    信じられないほどの眺めで動作します!!!!!!!どうもありがとう! –

    関連する問題