Questions tagged [angularjs-ng-form]

Nestable alias of form directive. HTML does not allow nesting of form elements. It is useful to nest forms, for example if the validity of a sub-group of controls needs to be determined.

AngularJS Site

Directive that instantiates FormController.

If the name attribute is specified, the form controller is published onto the current scope under this name.

Alias: ngForm In Angular forms can be nested. This means that the outer form is valid when all of the child forms are valid as well. However, browsers do not allow nesting of elements, so Angular provides the ngForm directive which behaves identically to but can be nested. This allows you to have nested forms, which is very useful when using Angular validation directives in forms that are dynamically generated using the ngRepeat directive. Since you cannot dynamically generate the name attribute of input elements using interpolation, you have to wrap each set of repeated inputs in an ngForm directive and nest these in an outer form element.

CSS classes

ng-valid Is set if the form is valid. ng-invalid Is set if the form is invalid. ng-pristine Is set if the form is pristine. ng-dirty Is set if the form is dirty. Submitting a form and preventing the default action Since the role of forms in client-side Angular applications is different than in classical roundtrip apps, it is desirable for the browser not to translate the form submission into a full page reload that sends the data to the server. Instead some javascript logic should be triggered to handle the form submission in an application-specific way.

For this reason, Angular prevents the default action (form submission to the server) unless the element has an action attribute specified.

You can use one of the following two ways to specify what javascript method should be called when a form is submitted:

ngSubmit directive on the form element ngClick directive on the first button or input field of type submit (input[type=submit]) To prevent double execution of the handler, use only one of the ngSubmit or ngClick directives. This is because of the following form submission rules in the HTML specification:

If a form has only one input field then hitting enter in this field triggers form submit (ngSubmit) if a form has 2+ input fields and no buttons or input[type=submit] then hitting enter doesn't trigger submit if a form has one or more input fields and one or more buttons or input[type=submit] then hitting enter in any of the input fields will trigger the click handler on the first button or input[type=submit] (ngClick) and a submit handler on the enclosing form (ngSubmit) Usage

This directive can be used as custom element, but be aware of IE restrictions.

as element:

136 questions
33
votes
1 answer

Angular ng-repeat with ng-form, accessing validation in controller

I am trying to generate an editable list using ng-repeat. I want to remind the user to update any edits before moving on, so I am using ng-form to create "nested" forms on the fly because the documentation says I can then use validation on these…
31
votes
4 answers

Getting form controls from FormController

I need a way to loop through the registered controls of an AngularJS form. Essentially, I'm trying to get all the $dirty controls, but there's no array of the controls (the FormController has a number of different properties/functions in addition…
18
votes
1 answer

Consider a form invalid while async validator promises are pending

I have an async validator: app.directive('validateBar', ['$http', function($http) { function link($scope, $element, $attrs, ngModel) { ngModel.$asyncValidators.myValidator = function(value) { return $http.get('/endpoint/' +…
Blaise
  • 13,139
  • 9
  • 69
  • 97
8
votes
2 answers

AngularJS form validation : $dirty value change through ui.router state changes

My use case : I have a multi step form using ui-router like in the plunkr below. I use ng-form to validate information provided by AngularJS, like $valid, $dirty etc. After each click on the "Next section" button, I send the form data to the server…
tomahim
  • 1,298
  • 2
  • 11
  • 29
8
votes
4 answers

Set AngularJS nested forms to submitted

I've got a nested AngularJS form like so:
sslepian
  • 1,881
  • 5
  • 21
  • 26
7
votes
1 answer

Validation doesnt work for File Input with 'Required' attribute- AngularJS

I have been playing around this and couldnt get it to work. I was creating an angular form and I was able to get the validation to work when required attribute is added to the text field. However, if a input type file is added with required…
Neel
  • 9,352
  • 23
  • 87
  • 128
6
votes
2 answers

validate nested forms without affecting the parent form

There is a validation problem in the nested forms in angularjs 1.5 and there is an issue in github about it. but 2 people in that topic offer the solution and one of them has open its way to the angularjs core which is ngFormTopLevel directive, and…
M.R.Safari
  • 1,857
  • 3
  • 30
  • 47
6
votes
1 answer

Remove ng-submitted after successful submission

I have a form that submits via an api and can be reused immediately after the post request has finished. For validation styling requirements I need to remove the css class ng-submitted from the I can't seem to find an angular method for…
James
  • 788
  • 2
  • 10
  • 28
6
votes
1 answer

AngularJS multiple forms with ng-repeat validation

I am using ng-form as a parent form and child mg-forms for validation with ng-repeat.
And validating like this: if…
Julius
  • 2,473
  • 3
  • 20
  • 26
5
votes
2 answers
5
votes
1 answer

Google-like search box with an AngularJS directive

I am writing an application which UI wise is almost exactly like Google. I arrive at landing page I have a search box which on submit directs you to results page. Here you have the same search box and other directives in which you can switch between…
Ela
  • 3,142
  • 6
  • 25
  • 40
5
votes
2 answers

How to reset custom input directive and its parent form to $pristine

I've implemented a custom input directive - counter with a reset capability. The directive has require: "ngModel". I am resetting the pristine state of the directive's ngModel with $setPristine(). Unlike $setDirty(), $setPristine() does not touch…
New Dev
  • 48,427
  • 12
  • 87
  • 129
4
votes
1 answer

AngularJS ng-form within ng-repeat

I have a page, with a number of items (rendered through ng-repeat), each one of these items holds a bunch of "read-only" data and a form. I'm using the ng-form directive and naming them as in this question/answer However where mine differs is I'm…
ry8806
  • 2,258
  • 1
  • 23
  • 32
3
votes
1 answer

ngForm simple example in angular 6 with select box

Angular 6 form validation simple example. With email validation and select box(drop down).ngForm simple example in angular 6 with select box
Jai Kumaresh
  • 715
  • 1
  • 7
  • 33
3
votes
1 answer

Angular NgForm with two components

I am trying to do a form with two different components. One parent and one child. I looked into a lot of tutorials and they all talked about doing a service. However, I found a method on this website. It consists in adding the following in the child…
PierBJX
  • 2,093
  • 5
  • 19
  • 50
1
2 3
9 10