I am just starting out with angular js and this login page is my first step. I have created a basic login form, but when I click on the login()
button, the form() does not get submitted and it keeps showing an error message saying "invalid username and password". I tried looking for solutions in other blogs but couldn't quite understand them.
Here is the code:
HTML :
<div class="package" ng-controller="credentials">
<form ng-submit="loginform()" class="ng-scope ng-pristine ng-valid">
<label form="emailinput">Email</label>
<input type="text" class="form-control ng-pristine ng-valid" ng-model="username">
<label form="pwdinput">Password</label>
<input type="password" class="form-control ng-pristine ng-valid" ng-model="password">
<div>
<button type="cancel" class="btn" ng-click="toggle_cancel()">Cancel</button>
<button class="btn btn-primary" ng-click="">Login</button>
</div><br/>
<span class="text-danger">{{ error }}</span>
</form>
Angular JS :
var app = angular.module('logapp',['ngRoute']);
app.config(function($routeProvider){
$routeProvider
.when('/login',{
templateUrl : "login.html",
controller : "loginctrl"
})
.when('/home',{
templateUrl : "home.html",
controller : "homectrl"
});
$routeProvider.otherwise({ redirectTo: '/login'});
});
app.controller('credentials',['$scope','$route','$http','$window',function($scope,$route,$http,$window){
$scope.templates =
[
{ url: 'login.html' },
{ url: 'practice.html'}
];
$scope.template = $scope.templates[0];
$scope.loginform = function (username, password) {
if ( username === 'admin' && password === '1234') {
authentication.isAuthenticated = true;
$scope.template = $scope.templates[1];
$scope.user = username;
} else {
$scope.error = "Invalid username and password";
};
};
app.factory('authentication', function() {
return {
isAuthenticated: false,
user: null
}
});
}]);