here is my app.js in which i have create a service which should check that if this particular username and password fills in login should be done and page should be redirected to home.html i am not able to get how to call that particular service into the controller and whether i have created the service correctly.
i also need to check that both the fields should not be empty is they gets empty submit button should gets disabled.
app.js
// create angular app
var validationApp = angular.module('validationApp',['ngRoute']);
validationApp.config(
function($routeProvider) {
$routeProvider
.when('/', {
templateUrl: 'main.html',
controller: 'mainController'
})
.when('/home', {
templateUrl: 'home.html',
controller: 'mainController'
})
.otherwise({
redirectTo: '/main.html'
});
});
validationApp.service('loginservice',function()
{
this.login = function($scope,$location){
var username = $scope.user.email;
var password = $scope.user.password;
if(username == "[email protected]" && password=="1234")
{
if ($scope.userForm.$valid) {
alert('thank you for submitting your form');
$location.path("/home");
}
}
else
{
alert("invalid username and password");
}
}
});
// create angular controller
validationApp.controller('mainController', function($scope,loginservice) {
$scope.dologin = loginservice.login();
});
//here is my index.html page
<body>
<div ng-app="validationApp">
<ng-view></ng-view>
</body>
//here is my main.html page
main.html
<div class="container">
<div class="row">
<div>
<form name="userForm" method="post" ng-submit="dologin()" novalidate>
<h1>Login Form</h1>
<div class="form-group" ng-class="{ 'has-error' : userForm.email.$invalid && !userForm.email.$pristine }">
<label>Email</label>
<input type="email" name="email" class="form-control" ng-model="user.email" value="[email protected]">
<p ng-show="userForm.email.$invalid && !userForm.email.$pristine" class="help-block">Enter a valid email.</p>
</div>
<div class="form-group" ng-class="{ 'has-error' : userForm.password.$invalid && !userForm.password.$pristine }">
<label>Password</label>
<input type="password" name="password" class="form-control" ng-model="user.password" value="1234">
<p ng-show="userForm.password.$invalid && !userForm.password.$pristine" class="help-block">Enter a valid password.</p>
</div>
<label>Click me to toggle: <input type="checkbox" ng-model="checked"></label><br/>
<button ng-model="button" ng-disabled="checked">Button</button>
<button type="submit" class="btn btn-primary" ng-disabled="userForm.$invalid" >Submit</button>
</form>
</div>
</div>
</div>
See if this works,
In your controller