I am relatively very new to AngularJS and javascript Please be merciful while answering.
I am trying to create one sample application where I want to nest controllers. MainController which will always be there acting as parent container which will render menu and user name on top if user is logged in. For now I am checking if user is stored in localStorage.
Each page will have its own controller which will do page specific things.
I am stuck at ng-submit and why its not working?
Any help is appreciated.
Index.html
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-route.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-resource.js"></script>
<script src="angularapp.js"></script>
</head>
<body ng-app="myAngularApp">
<div ng-controller="MainController">
<div ng-show="isLoggedIn">Menu</div>
<div ng-view>
</div>
</div>
</body>
login.html
<from ng-submit="login()">
<input type="text" id="username" ng-model="username"></input>
<input type="password" id="password" ng-model="password"></input>
<input type="submit" value="submit" id="submit" ng-submit="login()"></input>
</form>
angularapp.js
angular.module('myAngularApp',['ngRoute','ngResource'])
.config(['$routeProvider',function($routeProvider){
$routeProvider.when('/home',{
controller:'SubController',
templateUrl:'templates/home.html'
})
.when('/login',{
controller:'MainController',
templateUrl:'templates/login.html'
});
}])
.controller('MainController',['$scope','$window','userService',function($scope,$window,userService){
$scope.isLoggendIn=userService.isLoggedIn;
console.log('here I come');
$scope.username='';
$scope.password='';
$scope.login=function(){
alert('Ignored???');
$window.localStorage['myUser']={username:$scope.username,password:$scope.password};
consoel.log('user is logged in now');
};
}])
.controller('SubController',['$scope',function($scope){
$scope.myContent='Contenst to show after logged in';
}])
.service('userService',['$window',function($window){
var self=this;
self.isLoggedIn=function(){
if($window.localStorage['myUser'])
return true;
else
return false;
};
}]);
Please change your code. Seems you have made spelling mistake.
<from ng-submit="submit()">
to:
<form ng-submit="submit()">
Also replace the following code.
<input type="submit" value="submit" id="submit" ng-click="submit()"></input>
No need of ng-submit="login()" in this place type="submit is enought and last one is button not input
<from ng-submit="submit()">
<input type="text" id="username" ng-model="username"></input>
<input type="password" id="password" ng-model="password"></input>
<button type="submit" value="submit" id="submit"></button>
</form>
Edit your code:
<input type="submit" value="submit" id="submit" ng-submit="login()"></input>
Must be as below:
<input type="submit" value="submit" id="submit" ng-click="submit()"></input>
I think there is a problem in your login.html. remove login() because you have submit()
<from ng-submit="submit()">
<input type="text" id="username" ng-model="username"></input>
<input type="password" id="password" ng-model="password"></input>
<input type="submit" value="submit" id="submit"></input>
</form>
Related
I have this very basic example. This should work in a form for multiple inputs as well.
I want a form without any required parameters. But I only want to enable the button if at least one of them is not empty.
$pristine almost works. The problem is that if I type something and delete it the button remains enabled.
Basically,
If all fields are empty -> button disabled
Else if at least one not empty -> button enabled
function ctrl($scope){
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app>
<form name="myForm" ng-controller="ctrl">
<input type="text" ng-model="name" name='myName' ng-model='myName'/>
<span>{{name}}</span>
<input type="submit" value="Submit" ng-disabled="myForm.$pristine" />
{{myForm.$pristine}}
</form>
</div>
Pristine checks whether the input field is touched or not. That's why it remains enabled after you deleted your input. Check on valid and provide some condition. Or use pattern and regex.
With `required`:
function ctrl($scope) {
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app>
<form name="myForm" ng-controller="ctrl">
<input type="text" ng-model="name" name='myName' ng-model='myName' required/>
<span>{{name}}</span>
<input type="submit" value="Submit" ng-disabled="myForm.$invalid"/>
</form>
</div>
Without required using controller:
function ctrl($scope) {
$scope.isDisable = true;
$scope.validate = function(){
if($scope.name == undefined || $scope.name == ""){
$scope.isDisable = true;
} else {
$scope.isDisable = false;
}
}
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app>
<form name="myForm" ng-controller="ctrl">
<input type="text" ng-model="name" name='myName' ng-model='myName' ng-keyup = "validate()"/>
<span>{{name}}</span>
<input type="submit" value="Submit" ng-disabled="isDisable"/>
</form>
</div>
Another way using `name.length`:
function ctrl($scope) {
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app>
<form name="myForm" ng-controller="ctrl">
<input type="text" ng-model="name" name='myName'/>
<span>{{name}}</span>
<input type="submit" value="Submit" ng-disabled="!name.length"/>{{name.length}}
</form>
</div>
Note: If you don't want to use HTML5 validation that you can mark your form novalidate.
This is naive way, but you can just use ng-model of each input in ng-disabled
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app>
<form name="myForm" ng-controller="ctrl">
<input type="text" ng-model="name" name='myName' />
<input type="text" ng-model="fName" name='FirstName'/>
<span>{{name}}</span>
<input type="submit" value="Submit" ng-disabled="!(name || fName)" />
{{myForm.$pristine}}
</form>
</div>
Also, your code has 2 ng-model for the same input
Use disabled="!name.length" instead of ng-disabled="myForm.$pristine".
This will solve your issue. Below is working code:
function ctrl($scope) {
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app>
<form name="myForm" ng-controller="ctrl">
<input type="text" ng-model="name" name='myName' ng-model='myName' />
<span>{{name}}</span>
<input type="submit" value="Submit" ng-disabled="!name.length" />
{{myForm.myName.$pristine}}
</form>
</div>
I had a textfield to enter the emailId..Iam getting that value stored in $rootScope.emailId.
Now when I click the submit button,I must take to next page only if the emailId is entered or else it must give an alert saying that must be filled.
Currently it is in form and i am using required="true" and form.$valid but still its not working is there any other way to achieve this.
<form name="customerForm" class="form-horizontal">
<div class="form-group">
<label for="inputEmail" class="col-lg-4 col-lg-offset-1 control-label">Email address</label>
<div class="col-lg-4">
<input class="form-control" ng-model="$root.customerDetails.eMail" placeholder="Enter customer email" type="email" required focus value="{{emailId}}">
</div>
</div>
</form>
--
<div class="col-lg-1">
<button type="submit" class="btn btn-primary right-button" ng-click="$ctrl.proceedToPaymentDetails()" ng-disabled="$ctrl.customerDetails">Next</button>
</div>
Currently the form and the button are not in the same html page..Is der any way to fix this.
change ng-disabled="customerForm.$invalid" or you can also check the value of email id inside your proceedToPaymentDetails() method for alert message.
Please gone throw this example code for your better understanding
<!DOCTYPE html>
<html lang="en">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>
<div ng-app="myApp" ng-controller="formCtrl">
<form novalidate>
First Name:<br>
<input type="text" ng-model="user.firstName"><br>
Last Name:<br>
<input type="text" ng-model="user.lastName">
<br><br>
Email:<br>
<input type="text" ng-model="user.emailID">
<br><br>
<button ng-click="proceedToPaymentDetails()">Next</button>
</form>
<p>form = {{user}}</p>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('formCtrl', function($scope) {
$scope.user = {firstName:"", lastName:"",emailID:""};
$scope.proceedToPaymentDetails = function() {
alert($scope.user.emailID);
};
});
</script>
</body>
</html>
So i have a log in page, there is a form and what i wanna do is when the user enters their username and password, it checks it in an object (ik i should do it in backend but stay with me here) but the jquery function is not working. here is the code.
<div class="login">
<h1>Login</h1>
<form>
<input type="text" name="u" placeholder="ID" required="required" />
<input type="password" name="p" placeholder="Password" required="required" />
<button id="but" type="submit" class="btn btn-primary btn-block btn-large">Enter Workspace</button>
</form>
</div>
<script>
$('#but').click(function() {
alert("lol");
});
</script>
what do i neeed to fix in order for the jquery function to work?
What do you mean 'it checks it in an object'? Do you want to grab the username & password when a user clicks the button? If so then you'd do something like this:
$('#but').click(function() {
var username=$('input[name=u]').val();
var password = $('input[name=p]').val();
// Do checks with the values here e.g send to server for validation
});
Adding jquery in the script. Also you may write the jquery functions in $(document).ready() or $(function(){}) but it as optional as long as your write your script after the element it is trying to access.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="login">
<h1>Login</h1>
<form>
<input type="text" name="u" placeholder="ID" required="required" />
<input type="password" name="p" placeholder="Password" required="required" />
<button id="but" type="submit" class="btn btn-primary btn-block btn-large">Enter Workspace</button>
</form>
</div>
<script>
$(function(){
$('#but').click(function() {
alert("lol");
});
})
</script>
I'm new to JavaScript and I want to log in to a webpage. This code will take me to the web page but won't sign in. I know it knows the values of username/password because if I do alert(loginForm.elements["username"].value) it displays "someUsername".
I'm not sure how to get these values to be entered into the boxes and hit submit. Any advice helps, thanks!
<script>
function login() {
document.loginForm.action = "http://websiteToLoginTo.com";
document.loginForm.submit();
}
</script>
<body onLoad="login()">
<form name="loginForm" method="post">
<input type="hidden" name="username" value="someUsername">
<input type="hidden" name="password" value="somePassword">
</form>
</body>
The website I'm trying to connect to has this:
<form action="/Authn/UserPassword" method="post" name="loginForm">
<input id="username" name="t_username">
<input id="password" name="t_password">
<input type="submit" name="login" value="Login" class="submit">
</form>
i want to show a login form when a click is done on a link . i want to make default behaviour of div "hide ".. how can i do that ? may form is by default showing on page., please help me . it will b grate ful for you .
Sign In
<div class="form">
<input type="text" value="" name="myinput" id="myinput"/>
<input type="submit" name="submit" value="submit"/>
</div>
javascript:
$(document).ready (function() {
$('.signin').click(function() {
$('.form').show();
return false;
});
});
Add style to the "form" to hide it.
<div style="display:none;" class="form">
<input type="text" value="" name="myinput" id="myinput"/>
<input type="submit" name="submit" value="submit"/>
</div>
Set display property of form class to none as shown belo in using css.
Sign In
<div class="form">
<input type="text" value="" name="myinput" id="myinput"/>
<input type="submit" name="submit" value="submit"/>
</div>
css:
.form{
display:none;
}
javascript:
$(document).ready (function()
{
$('.signin').click(function()
{
$('.form').show();
$('.signin').hide();
});
});