Form in AngularJS

AngularJS complements the form of filling and validation features. You can use ng-click directive to handle the event by clicking on the button and using the flags $ dirty and $ invalid to make it effective. Use novalidate with the form declaration to disable the valicate feature of the form. The control section of the form uses an effective set of AngularJS events.

Events in AngularJS

AngularJS provides many events that can be linked to form controls of HTML. For example, ng-click usually attaches to a button. Here are the events supported by AngularJS.

  1. ng-click
  2. ng-dbl-click
  3. ng-mousedown
  4. ng-mouseup
  5. ng-mouseenter
  6. ng-mouseleave
  7. ng-mousemove
  8. ng-mouseover
  9. ng-keydown
  10. ng-keyup
  11. ng-keypress
  12. ng-change

Introducing ng-click directive in AngularJS

Reset the form's data using the ng-click directive of a button.

 ten = "ho" type = "text" ng-model = "Ho" required >  ten = "ten" type = "text" ng-model = "Ten" required >  ten = "email" type = "email" ng-model = "email" required >  ng-click = "reset ()" > Reset 

Validate data in AngularJS

Here are the variables used to track errors.

  1. $ dirty - Notice that the data is changed.
  2. $ invalid - Notice that the input is invalid.
  3. $ error - Accurate error message.

For example

Here is an example to illustrate the directives introduced above in the AngularJS application:

formtrongAngularjs.html

 Vi du Form in AngularJS  src = "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js" > 

 

 Ung Dung AngularJS  ng-app = "ungdungAngularjs" ng-controller = "sinhvienController" >  ten = "sinhvienForm" novalidate >  border = "0" > Nhap ho:  ten = "ho" type = "text" ng-model = "Ho" required >  style = " color : red " ng-show = "sinhvienForm.ho. $ dirty && sinhvienForm.ho. $ invalid" >  ng-show = "sinhvienForm.ho. $ error.required" > Ho la bat buoc. Note:  ten = "ten" type = "text" ng-model = "Ten" required >  style = " color : red " ng-show = "sinhvienForm.ten. $ dirty && sinhvienForm.ten. $ invalid" >  ng-show = "sinhvienForm.ten. $ error.required" > Ten la bat buoc. Email address:  ten = "email" type = "email" ng-model = "email" length = "100" required >  style = " color : red " ng-show = "sinhvienForm.email. $ dirty && sinhvienForm.email. $ invalid" >  ng-show = "sinhvienForm.email. $ error.required" > Dia chi Email is bat buoc.  ng-show = "sinhvienForm.email. $ error.email" > Dia Email is not available.  ng-click = "reset ()" > Reset  ng-disabled = "sinhvienForm.ho. $ dirty && sinhvienForm.ho. $ invalid || sinhvienForm.ten. $ dirty && sinhvienForm.ten. $ invalid || sinhvienForm.email. $ dirty && sinhvienForm.email. $ invalid" ng -click = "submit ()" > Submit 

Result:

Open the formtrongAngularjs.html page in the browser and see the results.

Form in AngularJS Picture 1

Follow tutorialspoint

Previous lesson: Modules in AngularJS

Next article: Include syntax in AngularJS

4 ★ | 1 Vote

May be interested

  • Multilingual (i18n) in AngularJSMultilingual (i18n) in AngularJS
    angularjs provides multi-lingual functions (i18n) with 3 types of filters, currency, date and number. we just need to combine the js corresponding to the location depending on the country. by default it will attach to the location in the web browser.
  • Expression in AngularJSExpression in AngularJS
    expression is used to bind application data to html tags. expression is written in {{expression}}. expression has a similar way of operating as the ng-bind directive.
  • Controller component in AngularJSController component in AngularJS
    an angularjs application works primarily based on the controller component to control the flow of data in the application.
  • MVC structure in AngularJSMVC structure in AngularJS
    model view controller or mvc is a popular call, a software design model for web-based applications.
  • Create the table in AngularJSCreate the table in AngularJS
    table data is often repeated in natural order. in angularjs, ng-repeat directive can be used to draw tables easily. below is an example of how to use ng-repeat to create tables.
  • Scope in AngularJSScope in AngularJS
    scope is a special javascript object with the role of linking controllers and views. scope contains information as model data. in the controller, model data can be accessed via the $ scope object.
  • Form - Form in CSSForm - Form in CSS
    forms - forms are an integral part of any kind of website. let's see how to build the display interface part of a basic form.
  • HTML formHTML form
    the form in html contains form elements - the element types that take input data such as filling in text fields, checkboxes, buttons, submit buttons ...
  • How to create a Google Form form on Google DriveHow to create a Google Form form on Google Drive
    google form on google drive form will help users create a table to collect information, comments, votes, or contact information of a group of subjects.
  • Include syntax in AngularJSInclude syntax in AngularJS
    html does not support embedding html web pages in html pages. to achieve this function, the following methods can be used: