在角度程序$位置给出错误

时间:2016-11-21 09:44:48

标签: javascript angularjs ngroute angularjs-ng-route

我有一个简单的程序,我试图通过检查用户名和密码来重定向使用。但是当条件成立时,我没有重定向,而是给出了错误。有人可以帮我吗?

片段:

<html>
<head>

 <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.5/angular.min.js"></script>
 <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.5/angular-route.min.js"></script>

</head>

<body ng-app="myApp" ng-controller="myCtrl">
        Username: <input type="text"  ng-model="username"><br>
        Password: <input type="password" ng-model="password"><br>
        <button ng-click="submit()">LogIn</button>

<script>

    //module declaration
    var app=angular.module("myApp",['ngRoute']);

    //routing declaration
    app.config(function($routeProvider, $httpProvider){

        $routeProvider
        .when('/login',{
            templateUrl: 'login.html'
        })
        .when('/dashboard',{
            templateUrl: 'dashboard.html'
        })
        .otherwise({
            templateUrl: 'login.html'
        });

    });

    //controller declaration
    app.controller("myCtrl",function($scope, $location){

            $scope.submit= function(){

                if($scope.username=='admin' && $scope.password=="admin")
                {
                    $location.path('/dashboard');
                    //alert("Hi");
                }

            };
    });

</script>
</body>
</html>

错误:

enter image description here

  

在XAMPP中运行文件夹

1 个答案:

答案 0 :(得分:1)

<html>

<head>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.5/angular.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.5/angular-route.min.js"></script>

</head>

<body ng-app="myApp" ng-controller="myCtrl">

  <ng-view></ng-view>
  Username:
  <input type="text" ng-model="username">
  <br>Password:
  <input type="password" ng-model="password">
  <br>
  <button ng-click="submit()">LogIn</button>

  <script>
    //module declaration
    var app = angular.module("myApp", ['ngRoute']);

     //routing declaration
app.config(function($routeProvider, $httpProvider){

    $routeProvider
    .when('/login',{
        templateUrl: 'login.html'
    })
    .when('/dashboard',{
        templateUrl: 'dashboard.html'
    })
    .otherwise({
        templateUrl: 'login.html'
    });

});

     //controller declaration
    app.controller("myCtrl", function($scope, $location) {

      $scope.submit = function() {

        if ($scope.username == 'admin' && $scope.password == "admin") {
          console.log("called")
          $location.path('/dashboard');
          //alert("Hi");
        }

      };
    });
  </script>
</body>

</html>

我想你忘了给ng-view部分(dashboard.html)加载。我没有在你的代码中看到任何$ location错误。