KISHAN PATI KISHAN PATI - 4 months ago 46
AngularJS Question

Controller is not getting called in angularjs

I am working on angular js app. I am unable to call my controller in my app on click of button submit. Please check below sample code:

index.html

<!DOCTYPE html>
<html lang="en" ng-app="adminsuite">
<head>
<meta charset ="utf-8">
<title></title>
</head>
<body>
<script src="bundle.js" charset="utf-8"></script>
<div class="container">
<div ui-view></div>
</div>


</body>
</html>


login.html

<div class="col-md-6 col-md-offset-3">
<h2>Login</h2>
<form name="form" ng-submit='login()' role="form">
<div class="form-group" ng-class="{ 'has-error': form.username.$dirty && form.username.$error.required }">
<label for="username">Username</label>
<input type="text" name="username" id="username" class="form-control" ng-model="username" required />
<span ng-show="form.username.$dirty && form.username.$error.required" class="help-block">Username is required</span>
</div>
<div class="form-group" ng-class="{ 'has-error': form.password.$dirty && form.password.$error.required }">
<label for="password">Password</label>
<input type="password" name="password" id="password" class="form-control" ng-model="password" required />
<span ng-show="form.password.$dirty && form.password.$error.required" class="help-block">Password is required</span>
</div>
<div class="form-actions">
<button type="button" ng-disabled="form.$invalid" class="btn btn-primary">Login</button>
</div>
</form>




index.js

window.onpopstate = function (e) { window.history.forward(1); }
require('jquery/dist/jquery.js');
require('bootstrap/dist/css/bootstrap.css');
require('./content/common.css');
require('angular');
require('angular-ui-router/release/angular-ui-router.js');
require('angular-route/angular-route.js');
require('angular-cookies/angular-cookies.js');
angular.module('adminsuite', ['ui.router','ngCookies']).config(function($stateProvider, $urlRouterProvider) {

$urlRouterProvider.otherwise('/');
$stateProvider
.state('login', {
url: '/',
templateUrl: 'Login/login.html',
controller: 'loginController'
})
// HOME STATES AND NESTED VIEWS ========================================
.state('dashboard', {
url: '/dashboard',
templateUrl: 'views/dashboard.html',
});

// ABOUT PAGE AND MULTIPLE NAMED VIEWS =================================

});
require('./Login/loginController.js');

require('./services/loginAuthenticationService.js');
require('./services/UserServices.js');


loginController.js

angular
.module('adminsuite')
.controller('loginController', ['$location', '$rootScope', 'AuthenticationService', '$scope', '$state', function($location, AuthenticationService, $scope, $state) {
$scope.login = function(){
$scope.dataLoading = true;
AuthenticationService.Login($scope.username, $scope.password, function (response) {
if (response.success) {
AuthenticationService.SetCredentials($scope.username, $scope.password);
console.log('Login successful');
//$rootScope.state = true;
$state.go('dashboard');
} else {
console.log(response.message);
$scope.dataLoading = false;
//$rootScope.state = false;
}
});
};
}


]);

I am not getting any error in console section of browser or in command prompt.

Answer

Edit the login button in login.html to have type="submit" instead of type="button":

<button type="submit" ng-disabled="form.$invalid" class="btn btn-primary">Login</button>
Comments