-1

I have a simple program where I am trying to redirect use using a check on username and password. But when condition holds true I am not redirected but instead given an error. Can someone help me out?

SNIPPET:

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

ERROR:

enter image description here

Running the folder in XAMPP

Deadpool
  • 7,811
  • 9
  • 44
  • 88

1 Answers1

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>

I think you forgot to give ng-view where partials(dashboard.html) will be loaded.And i didnt see any $location errors in your code.

Nikhil Mohanan
  • 1,260
  • 1
  • 12
  • 23