【问题标题】:In angular program $location giving error在角度程序中 $location 给出错误
【发布时间】:2017-04-04 15:58:56
【问题描述】:

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

片段:

<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>

错误:

在 XAMPP 中运行文件夹

【问题讨论】:

标签: javascript angularjs ngroute angularjs-ng-route


【解决方案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 将在哪里加载 partials(dashboard.html)。而且我没有在你的代码中看到任何 $location 错误。

【讨论】:

  • 运行代码时不要忘记传递 login.html、dashboard.html 和 login.html 文件。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-03-25
  • 2017-10-14
  • 1970-01-01
  • 1970-01-01
  • 2017-03-05
  • 1970-01-01
  • 2017-05-07
相关资源
最近更新 更多