简体   繁体   中英

Angular JS Redirecting to another page

I am working on angular js on a login form. If user name and password is correct then it should redirect to a success page. I am displaying a message for now but got struck on how to redirect to a success page once user enter correct credentials. any help ?

LOGIN.html :

<!DOCTYPE html>
<html ng-app="">
<head>
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js"></script>
    <script src="Controller.js"></script>
</head>
<body ng-controller="LoginCheckController">
    <h1>Login Page</h1>
    <hr />
    UserName : 
    <input type="text" ng-model="txtUname" required/><br />
    Password  :
    <input type="password" ng-model="txtPwd" required/><br />

    <input type="submit" value="Submit" ng-click="LoginCheck()" /><br />
    <!--    <button ng-click="go('/Successpage')">Submit</button>-->
    <br />
    <div ng-repeat="x in users">

        <div ng-if="txtUname === x.UserName && txtPwd === x.Password">
            You entered UserName and Password correctly.
        </div>

        <div ng-if="txtUname === '' && txtUname !== x.UserName && txtPwd !== x.Password">
            You entered UserName and Password Incorrectly.
        </div>
    </div>


CONTROLLER . js :

function LoginCheckController($scope) {

            $scope.users = [
    { UserName: 'chandra', Password: 'hello' },
    { UserName: 'Harish', Password: 'hi' },
    { UserName: 'Chinthu', Password: 'hi' },
            ];
            $scope.go = function (path) {
                $location.path("/SuccessPage");
            };                 

        }



REGISTRATION . html :


<!DOCTYPE html>
<html>
<head>
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js"></script>
    <script src="RegistrationController.js"></script>
</head>

<body>
    <h1>Registration Page</h1>
    <hr />
    <div ng-app="myApp">
        UserName : 
        <input type="text" ng-model="Uname" /><br />
        New Password  :
        <input type="password" ng-model="Pwd" /><br />
        Re-enter Password  :
        <input type="password" ng-model="Pwd" /><br />

        <div ng-controller="DemoController">
            <div>
               Gender : <select ng-model="correctlySelected" ng-options="opt as opt.label for opt in options">
                </select>               
            </div>
            <br />

          Address : <br />
            <textarea rows="3" cols="20"></textarea>

            <div>
                State : <select ng-model="correctlySelected" ng-options="opt as opt.label for opt in states">
                </select> 
            </div>

            <div>
                City : <select ng-model="correctlySelected" ng-options="opt as opt.label for opt in cities">
                </select> 
            </div>       
            <input type="Submit" name="btn" value="Register" />
        </div>
    </div>
</body>
</html>


REGISTRATIONCONTROLLER . js :

angular.module('myApp', []).controller('DemoController', function ($scope) {

    $scope.options = [
      { label: 'Male' },
      { label: 'Female' }
    ];

    $scope.cities = [
      { label: 'Mahabubnagar' },
      { label: 'Ranga Reddy' },
      { label: 'Nalgonda' },
      { label: 'Vizag' },
      { label: 'Karnool' },
      { label: 'Kadapa' },
      { label: 'Chennai' },
      { label: 'abc' },
      { label: 'xyz' }
    ];

    $scope.states = [
     { label: 'Telangana' },
     { label: 'Andhrapradesh' },
     { label: 'Tamilanadu' },     
    ];

});

Here is the plunker, I think this will help you. Thanks

http://plnkr.co/edit/XraWvTaR4fGF3Rdo6KBu?p=preview

app.config(function($stateProvider, $urlRouterProvider) {

  // For any unmatched url, send to /index
  $urlRouterProvider.otherwise("/login");

  $stateProvider
    .state('login', {
      url: "/login",
      templateUrl: "login.html",
      controller: "LoginCheckController"
    })
    .state('SuccessPage', {
      url: "/SuccessPage",
      templateUrl: "SuccessPage.html",
      //controller: "LoginCheckController"
    });
});

I hope this helps

function test(username,password,$location){
if(username='admin' && password='password'){
$location.path("success page");
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM