1

I am new to Angular JS and was wanting to start with the login form. I read a few online materials and decided to give it a go.

Below is my code, and when I click "Login", nothing happens, I am not sure if my html script talks to the controller or did I code something wrong in controller.

<!DOCTYPE html>
<html data-ng-app="loginAuth">
	<head>
		<title>
		</title>
	</head>
	<body>
	
	<h2>Angular Authentication</h2>

	<div data-ng-controller="loginController">
		<label>Username</label>
		<input type="text" name="username" data-ng-model="login.username"/>
		<br/>
		
		<label>Password</label>
		<input type="password" name="password" data-ng-model="login.password"/>	
		<br/>
		
		<input type="button" ng-submit="login.submit(login.username, login.password)" value="Login"/>
		<br/><br/>
		{{login.message}}
	</div>
	
	<script src="angular.min.js"></script> 
	
	<script>
	
	'use strict';
	
	angular.module('loginAuth')
	.controller('loginController', function ($scope)
	{
	
	function login.submit(username, password)
	{
		if (username === 'admin' && password === 'test99')
		{
		 .then(onSuccess);
		}
		 else
		 {
		 .catch(onError);
		 }
	}
	
	function onSuccess()
	{
		login.message = 'Login Successful!';
	}
	function onError(reason)
	{
		login.message = reason.message;
	}

	});
	 
	</script>
	</body>

</html>

Can someone please help me out.

2 Answers 2

1

You app should be define in proper way.

angular.module('loginAuth', [])

Also you need to define your controller methods correctly

Controller

angular.module('loginAuth')
.controller('loginController', function($scope) {
  $scope.login = {};
  $scope.login.submit = function(username, password) {
    if (username === 'admin' && password === 'test99') {
      //.then(onSuccess); //this is something wiered
    } else {
      //.catch(onError); //this is something wiered
    }
  }

  function onSuccess() {
    $scope.login.message = 'Login Successful!';
  }

  function onError(reason) {
    $scope.login.message = reason.message;
  }

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

3 Comments

I had changed my code to look like something below, still am unable to get through the issue angular.module('loginAuth', []) .controller('loginController', function ($scope) { function login.submit(username, password) { if (username === 'admin' && password === 'test99') { login.message = 'Login Successful!'; } else { login.message = 'Login failed!'; } }
you have declared function in wrong way..please look at the code which i added in my answer.
Thanks for this. I see the issue, the controller seems to load fine now in the webpage and I don't seethe {{login.message}} bind on screen. But however, the data binding doesnt seem to work upon successful authentication or a failed authentication. Sorry if am asking too much but I would like to get my understanding right on the basics as I learn them
0

I made changes to your original code to make it work. Hopefully this will help you learn some Angular :)

<!DOCTYPE html>
<html data-ng-app="loginAuth">
	<head>
		<title>
		</title>
	</head>
	<body>
	
	<h2>Angular Authentication</h2>

	<div data-ng-controller="loginController">
		<label>Username</label>
		<input type="text" name="username" data-ng-model="login.username"/>
		<br/>
		
		<label>Password</label>
		<input type="password" name="password" data-ng-model="login.password"/>	
		<br/>
		
		<input type="button" ng-click="login.submit(login.username, login.password)" value="Login"/>
		<br/><br/>
		{{login.message}}
	</div>
	
	<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular.min.js"></script> 
	
	<script>
	
	'use strict';
	
	angular.module('loginAuth', [])
	.controller('loginController', function ($scope) {
    $scope.login = {};
	$scope.login.submit = function(username, password)
	{
		if (username === 'admin' && password === 'test99')
		{
		 onSuccess();
		}
		 else
		 {
		 onError({message:'login failed'});
		 }
	}
	
	function onSuccess()
	{
		$scope.login.message = 'Login Successful!';
	}
	function onError(reason)
	{
		$scope.login.message = reason.message;
	}

	});
	 
	</script>
	</body>

</html>

1 Comment

Thanks! That worked, I see that I had used ng-submit, rather than ng-click for the button event. Thank you again!

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.