2017-01-30 13 views
-1

這是登錄HTML </SCRIPT> - >我們如何使用expressjs中的angularjs將頁面重定向到另一個頁面。它被重定向(獲得未被捕獲的錯誤)

 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> 

     <!-- Optional theme --> 
     <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous"> 

      <title>Login Page</title> 
     </head> 
     <body> 

     <div class="container" ng-controller="loginctrl"> 
     <h1>Login Page</h1> 
     <label>UserName</label><input class="form-control" ng-model="user.name"><br><br> 
     <label>Password</label><input class="form-control" ng-model="user.password"><br><br> 
     <button class="btn btn-primary" ng-click="login()" >Login</button> 
     </div> 

     <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular.min.js"></script> 
     <script src="controllers/logincontroller.js"></script> 
     <script src="angularjs/angular.js"></script> 
     <script src="angular-route/angular-route.js"></script> 
     </body> 
     </html>     

    This is Controller page 

     var myapp = angular.module('myApp', ['ngRoute']); 

     myapp.config(["$routeProvider", function($routeProvider) { 
      $routeProvider 
      .when('/', { 
       controller: 'loginctrl', 
       templateUrl: 'login.html' 
      }) 
      .when('/addusers', { 
       controller: 'addusersctrl', 
       templateUrl: 'addusers.html' 
      }) 
      .otherwise({ 
       redirectTo: '/' 
      });    
     }]); 

     myapp.controller('loginctrl',['$scope', '$route', '$routeParams', '$location',function($scope,$http,$location){ 
      console.log("im a controller");    

      $scope.login = function() { 
       //console.log($scope.user); 

       // $http.post('/login',$scope.user).success(function(response){ 
       // console.log(response); 
       // }); 

       $http({ 
         method: 'post', 
         url: '/login', 
         data: $scope.user 
        }).then(function successCallback(response) { 
         // this callback will be called asynchronously 
         // when the response is available 

         console.log(response.data); 
         //$window.location.href = 'addusers.html'; 
         $location.path("/addusers"); 
        }, function errorCallback(response) { 

         console.log(response.data); 
         // called asynchronously if an error occurs 
         // or server returns response with an error status. 
        });    
      }; 
     }]);    

     myapp.controller('addusersctrl',['$scope','$http',function($scope,$http){ 
      console.log("im a controller");   

      $scope.login = function() { 
       //console.log($scope.user); 

       // $http.post('/login',$scope.user).success(function(response){ 
       // console.log(response); 
       // }); 

       $http({ 
         method: 'post', 
         url: '/login', 
         data: $scope.user 
        }).then(function successCallback(response) { 
         // this callback will be called asynchronously 
         // when the response is available 

         console.log(response.data); 
         //$window.location.href = 'addusers.html'; 

        }, function errorCallback(response) { 

         console.log(response.data); 
         // called asynchronously if an error occurs 
         // or server returns response with an error status. 
        });    
      }; 
     }]); 

每當我試圖運行此代碼我得到

Angular.js error angular.js:38 Uncaught Error: [$injector:modulerr] http://errors.angularjs.org/1.6.1/ $injector/modulerr?p0=myApp&p1=Error%3A%2…ogleapis.com%2Fajax%2Flibs%2Fangularjs%2F1.6.1%2Fangular.min.js%3A21%3A332)

+0

[** my answer **](http://stackoverflow.com/a/41933573/4927984)是否解決了您的問題? – Mistalis

回答

0

您在dependency injection(第一個控制器)中遇到問題:

myapp.controller('loginctrl', ['$scope', '$route', '$routeParams', '$location', 
       function($scope, $http, $location) { 

你沒有注入相同的東西。將其更改爲:

myapp.controller('loginctrl', ['$scope', '$route', '$http', '$routeParams', '$location', 
       function($scope, $route, $http, $routeParams, $location) { 
相關問題