I can never seem to get Forms to work properly in Angular. I always add a <form name="myform"> and then try to access it either in the template or in the controller using $scope.myform but it is always undefined! I can never seem to figure out exactly how these work.
Here is my code:
Template
<form name="o365form" novalidate>
<ul class="formList">
<li>
<label>Office 365 Admin Username</label>
<input type="text" ng-model="Master.Start.skAccount.username" required/>
</li>
<li>
<label>Office 365 Admin Password</label>
<input type="text" ng-model="Master.Start.skAccount.password" required/>
</li>
</ul>
</form>
Then I have this code in a function in my controller, but o365form is undefined...
Controller
if ($scope.o365form.$invalid) {
return false;
} else {
return true;
}
You need to use to ng-form directive with this:
This is as simple as
<ng-form name="o365form">
<ul class="formList">
<li>
<label>Office 365 Admin Username</label>
<input type="text" ng-model="Master.Start.skAccount.username" required/>
</li>
<li>
<label>Office 365 Admin Password</label>
<input type="text" ng-model="Master.Start.skAccount.password" required/>
</li>
</ul>
</ng-form>
Add ng-submit directive in form
<form name="o365form" ng-submit="submit(o365form)" novalidate>
<ul class="formList">
<li>
<label>Office 365 Admin Username</label>
<input type="text" ng-model="Master.Start.skAccount.username" required/>
</li>
<li>
<label>Office 365 Admin Password</label>
<input type="text" ng-model="Master.Start.skAccount.password" required/>
</li>
</ul>
</form>
in controller
$scope.submit = function(o365form){
if (o365form.$valid) {
return false;
} else {
return true;
}
}
Example with $watch
<form name="o365form" form-valid novalidate>
<ul class="formList">
<li>
<label>Office 365 Admin Username</label>
<input type="text" ng-model="o365form.username" required/>
</li>
<li>
<label>Office 365 Admin Password</label>
<input type="text" ng-model="o365form.password" required/>
</li>
</ul>
<button type="submit">Submit</button>
</form>
Directive
app.directive("formValid", function () {
return {
link: function ($scope, element, attrs){
$scope.$watch(attrs.name, function (isValid){
if (isValid.$valid) alert("valid");
else alert("not valid");
}, true);
}
}
});
DEMO
Related
I need to set these three inputs disable
<input type="text" class="col-md-6" ng-model="a.one" name=""/>
<input type="number" class="col-md-6" ng-model="a.two" name="" ng-disabled="cm" required="required"/>
<div class='input-group date' id='date'>
<input type='text' ng-model="a.three" name="toDate" class="form-control" style="height:30px;" ng-disabled="cm" />
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
when I select one value from below input:
<select class="col-md-6 dropdown-height" ng-model="a.select" name="" ng-options="ms for ms in section" required="required"></select>
the option values inside the select input are coming from an api
scope.mapping = function () {
ApiServices.getAllValues().then(
function (response) {
scope.map= response.data;
});
};
How should I do it?
<input ng-disabled="a.select==='your_value'">
Do this for the three inputs.
Two ways you can do it.
1. apply ng-disabled="a.select" to the element you want to disable.
2. This is much better if you want to apply to more input fields.
Wrap your html inside fieldset and apply ng-disabled. Below is the working code
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope){
$scope.cm = true;
$scope.enable= function() {
$scope.cm = !$scope.cm;
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
<fieldset ng-disabled="cm">
<input type="text" class="col-md-6" ng-model="a.one" name=""/>
<input type="number" class="col-md-6" ng-model="a.two" name="" required="required"/>
<div class='input-group date' id='date'>
<input type='text' ng-model="a.three" name="toDate" class="form-control" style="height:30px;" />
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
</fieldset>
<button ng-click="enable()">enable fields</button>
</div>
I want to show the required error message once the from is submitted but not sure how to do it i am trying as shown below
<div class="controls">
<form name="formx" >
<ul class="form small">
<div class="tag">ID</div>
<input type="text" class="small" name="enrolmentId" ng-model="enrolmentDetail.Id" required="" onkeypress='return !(event.charCode == 32 )'>
<div class="error text-danger" style="position: absolute;margin-left: 120px;" ng-show="formx.enrolmentId.$error.required && (formx.$submitted || formx.enrolmentId.$touched)"><p>Id is required</p></div>
</li>
<li>
<div class="btn" ng-click="saveDetails(enrolmentDetail)" ng-show="formAction=='save'">SAVE</div>
</li>
</ul>
</form>
</div>
here now the error message is shown once we dirty the textbox but i want to show the error message once the form is submitted and if there is required error from should not be submitted
trying used ng-submit but not sure how to do it
Please help by creating a fiddle or posting a example to do it
This is an example
html
<body ng-app="myApp">
<div ng-controller="myCtrl as mc">
<form class="form" name="myForm" ng-submit="mc.submit(myForm)" novalidate>
<div class="form-group">
<label for="username">Username</label>
<input name="username" class="form-control" type="text" ng-model="mc.username" required/>
<p class="text-danger" ng-show="myForm.username.$error.required && mc.submitted">Username is required</p>
</div>
<div class="form-group">
<label for="password">Password</label>
<input name="password" class="form-control" type="password" ng-model="mc.password" required/>
<p class="text-danger" ng-show="myForm.password.$error.required && mc.submitted">Password is required</p>
</div>
<button class="btn btn-success">submit</button>
</form>
<p class="text-success" ng-show="mc.sent && mc.submitted">Form sent</p>
js
angular.module("myApp",[])
.controller("myCtrl", function(){
var vm = this;
vm.submit = submit;
function submit(form){
vm.submitted = true;
if(form.$valid && vm.submitted === true){
//Send data logic
vm.sent = true;
}
}
I get the correct record from the firebase array using the $getRecord but I can not save the changes made in the input fields. Our Add Controller works. What have we done wrong? Can anyone help? Thank you!
Edit Controller
app.controller("EditRecipeController", function($scope, $location, $routeParams, $firebaseArray) {
$scope.recipes.$loaded().then(function(recipeid) {
$scope.recipe = recipeid.$getRecord($routeParams.id);
$scope.editRecipe = function(recipe) {
$scope.recipes.$save(recipe).then(function() {
})};});});
Add Controller
app.controller("AddRecipeController", ["$scope", "createRecipes", function($scope, createRecipes) {
$scope.recipes = createRecipes;
$scope.addRecipe = function() {
$scope.recipes.$add({
title: $scope.title,
lead: $scope.lead,
keyword: $scope.keyword,
ingredient: $scope.ingredient,
instructions: $scope.instructions
});
alert("Your recipe has been succefully saved!")
// reset the recipe input
$scope.recipe = "";
};}]);
Factory
app.factory("createRecipes", ["$firebaseArray",function($firebaseArray) {
var ref = new Firebase("https://fiery-inferno-8595.firebaseio.com/recipes/");
return $firebaseArray(ref);}]);
HTML
<section id="{{recipe.$id}}" class="recipe-description editor">
<form ng-submit="addRecipe()">
<section class="headColumn">
<div class="mySearch">
<span>My search: </span>
<span ng-bind="search.$"></span>
</div>
<h2>
<input type="text" ng-model="title" name="recipeTitle" ng-value="recipe.title" required="">-
<input type="text" ng-model="lead" name="recipeLead" ng-value="recipe.lead" required="">
</h2>
<ul class="keywords">
<li><label></label>Categories: </li>
<li ng-repeat="keyword in keywords track by $index">
<input type="text" ng-model="keywords[$index]" name="recipeKeyword" ng-value="recipe.keywords" placeholder="Add a keyword">
<button class="remove-field" ng-show="$last" ng-click="removeKeyword()">-</button>
</li>
<button class="add-field" ng-click="addKeyword()">+</button>
</ul>
</section>
<section class="sideColumn">
<h4>Ingredients</h4>
<ul class="ingredients">
<li ng-repeat="ingredient in ingredients track by $index">
<input type="text" ng-model="ingredients[$index]" name="recipeIngredient" ng-value="recipe.ingredients" placeholder="Add an ingredient">
<button class="remove-field" ng-show="$last" ng-click="removeIngredient()">-</button>
</li>
<button class="add-field" ng-click="addIngredient()">+</button>
</ul>
</section>
<section class="mainColumn">
<div class="readytime">
<h4>Time until ready</h4>
<ul>
<li>
<span>Preperation Time: </span>
<input type="text" ng-model="preptime" name="recipePreptime" ng-value="recipe.preptime" placeholder="Add preperation Time">
</li>
<li>
<span>Cooking Time: </span>
<input type="text" ng-model="cookingtime" name="recipeCookingtime" ng-value="recipe.cookingtime" placeholder="Add cooking Time">
</li>
</ul>
</div>
<div class="instructions">
<h4>Instructions</h4>
<!--TO DO: This input should work like textarea -->
<input type="text" ng-model="instructions" name="recipeInstructions" ng-value="recipe.instructions">
</div>
</section>
<button ng-click="addRecipe()" ng-hide="recipe" type="submit">Save the Recipe</button>
<button ng-click="editRecipe()" type="submit" ng-show="recipe">Update the Recipe</button>
</form>
You're edit function:
$scope.editRecipe = function(recipe){
$scope.recipes.$save(recipe).then(function(){});
}
is expecting the "recipe" to be passed in, but I don't see it being passed in anywhere. It looks like you are saving the current recipe to:
$scope.recipe
so if $scope.recipe has the correct object, I think you just have to save the scoped recipe opposed to passing it in.
$scope.editRecipe = function(){
$scope.recipes.$save($scope.recipe).then(function(){});
}
i have a problem while accessing $scope inside controller's behavior. code is like below.
<body id="main_body" ng-controller="FormController as frmCtrl">
<form id="form_991905" class="appnitro" name="loginForm" ng-submit="loginForm.$valid && frmCtrl.doLogin()" novalidate>
<div class="form_description">
<h2>Login Form</h2>
</div>
<ul>
<li id="li_1" >
<label class="description" for="username">Username </label>
<div>
<input name="username" class="element text medium crequired email" type="email" ng-model="login.username" form-validator />
<div class="errBx"></div>
</div>
</li>
<li id="li_2" >
<label class="description" for="password">Password </label>
<div>
<input name="password" class="element text medium crequired" type="text" ng-model="login.password" form-validator/>
<div class="errBx"></div>
</div>
</li>
<li class="buttons">
<input id="saveForm" class="button_text" type="submit" name="submit" value="Submit"/>
</li>
</ul>
</form>
i want to access the $scope.login.username inside the method
controller code.....
this.login = function(){
console.log($scope.login.username);
}
// controller code
You are using "controllerAs" syntax, so there is no $scope available. In the view you can access it via frmCtrl.login.username or in the contoller you can try this.login.username.
You can learn more about it here: http://www.johnpapa.net/angularjss-controller-as-and-the-vm-variable/
You're using ctrl as syntax, therefore your ng-model show be as follows:
ng-model="frmCtrl.login.password"
And function should change to:
this.login = function(){
console.log(this.login.username);
}
Here's FIDDLE
Not able to understand that why the required validation is not working ??
HTML
<div ng-app="app">
<div ng-controller="myctrl">
<form name="myform" class="form form-horizontal" novalidate>
<fieldset class="fieldset" ng-show="payment == 'bankAccount'" class="form-group clearfix">
<ul class="form-group">
<li ng-class="{
'has-error': myform.routingNumber.$invalid,
'has-success':myform.routingNumber.$valid}">
<label for="routingNumber">Bank Routing Number</label>
<div class="" ng-show="myform.routingNumber.$error.required"> <span class="help-block">Please enter routing number</span>
</div>
<input type="text" name="routingNumber" class="form-control" required/>
</li>
</ul>
</fieldset>
</form>
</div>
</div>
JS
var app = angular.module('app', [])
You need to provide an ng-model for the angular validation on the form to kick in.
Try:-
<input type="text" ng-model="routingNumber" name="routingNumber" class="form-control" required/>
On a side note:- there is no use of using label for without using id on the target input.
Demo