0

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' },     
    ];

});
3
  • have you maintain route defination? Commented Sep 9, 2014 at 6:08
  • no how can I do that? Commented Sep 9, 2014 at 6:08
  • could you bring it in fiddle ,its really hard to read Commented Sep 9, 2014 at 6:17

2 Answers 2

5

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"
    });
});
Sign up to request clarification or add additional context in comments.

Comments

0

I hope this helps

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.