0

i have a login page which evaluates username and password using angularjs but it seems that my conditional statement in the controller returns false even if i put a true value

here's my html file

<div class="row">
<form action="/">

    <input type="text" class="form-control" id="username" ng-model="username" required>                     
    <input type="Password" class="form-control" id="username" ng-model="password" required>                     
    <br>
    <button type="button" style="width: 40%;"  ng-click="submit()"> Log In </button>

</form>     
</div>

and my controller

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

app.controller('ctrl',
 function($scope, $location) { 

    $scope.submit = function() {
        var username = $scope.username;
        var password = $scope.password;

        if($scope.username == 'admin' && $scope.password == 'admin') {
            console.debug('success');
        } else {
            console.debug('fail');
        }
    }


});

every time i input 'admin' in username and password i always get 'fail' in my console which means the submit function returns false . .

3
  • 1
    <input type="Password" class="form-control" id="username" ng-model="password" is that right? should the id not be password? Commented Nov 10, 2016 at 14:21
  • opps type thats should be id="password" but i think thats not causing the problem since im not using id atm if im not wrong Commented Nov 10, 2016 at 14:25
  • can you make a plunkr illustrating your issues Commented Nov 10, 2016 at 14:28

2 Answers 2

1

1) Per the HTML spec you need to cannot have the same id on 2 different elements html 4.1

2)You should explicitly declare the objects you want your control to have injected like below. That way when you minifiy your code, the Dependency injection will continue to work

app.controller('ctrl',['$scope','$location'
 function($scope, $location) { }]);

3) TO answer your question why isnt this working. My plunkr works. I removed the routing, because it wasnt needed. Make sure you are typing in 'admin' no spaces/caps

Sign up to request clarification or add additional context in comments.

Comments

0
<html>
<head>
    <script src="angular.js"></script>
    <script src="route.js"></script>
</head>
<body>
    <div class="row" ng-app="myApp" ng-controller="ctrl">
        <form action="/">

            <input type="text" class="form-control" id="username" ng-model="username" required>                     
            <input type="Password" class="form-control" id="username" ng-model="password" required>                     
            <br>
            <button type="button" style="width: 40%;"  ng-click="submit()"> Log In </button>

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

        app.controller('ctrl',
                       function($scope, $location) { 

            $scope.submit = function() {
                var username = $scope.username;
                var password = $scope.password;

                if($scope.username == 'admin' && $scope.password == 'admin') {
                    console.debug('success');
                } else {
                    console.debug('fail');
                }
            }
        });
    </script>
</body>

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.