alvin manarya
alvin manarya

Reputation: 21

if statement in angularjs is not working

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

Upvotes: 0

Views: 585

Answers (2)

Satyapriya Mishra
Satyapriya Mishra

Reputation: 130

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

Upvotes: 0

gh9
gh9

Reputation: 10703

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

Upvotes: 1

Related Questions