Search code examples
javascriptangularjsvalidationangular-validation

How to make angular.js validation work inside a <form> tag


I wrote this code but it doesn't work when I place my app inside of <form name="myForm">... tag. What should I do to make it works! (whenever I place myForm inside of ng-app it works, but my app should be inside of <form>)

<form name="myForm">
    <div ng-app ng-init="myInput = 'ABC'">
        <input name="myInput" ng-model="myInput" required>
        <h1>{{myForm.myInput.$valid}}</h1>
    </div>
</form>

jsfiddle


Solution

  • You can use the ngForm directive (here) in order to enable the validation services within it:

    Like this:

    <div ng-app="" ng-controller="validateCtrl" name="myForm" novalidate ng-form>