How to add form validation which populated using directive in angular js?

Am developing a registration form in AngularJS . In which am reusing the same form at 4 different places. For that am using a common form at single html page like the below.

<div>
   <div>
        <div class="label-div float-left">
            <label>
                Address:
             </label>
            <input type="text" ng-model="addressInfo.addressLine1" />
        </div>
   </div>
</div>

In my registration using the directive of AngularJS am reusing it in 4 different place like below

<registration-form></registration-form>
<input type="button" value ="continue" >

How can i disable button in registration html since my directive template has my form content .

Answers


I would place the submit button inside the directive, and handle the ng-click event of the submit button by passing it through the scope of the directive

scope:{
onSubmit;'&'
}

inside your directives link function:

scope.submit=function(){
scope.onSubmit();
}

and your directive template:

<div>
   <form name="contactsForm">
        <div class="label-div float-left">
            <label>
                Address:
             </label>
            <input type="text" ng-model="addressInfo.addressLine1" />
            <input type="button" value ="continue" ng-click="submit()" ng-disabled="contactsForm.$invalid" >
        </div>
   </form>
</div>

usage:

<registration-form on-submit="postToServer()"></registration-form>

and in your parent controller, put any logic you want to execute when the form is submitted:

$scope.postToServer=function(){
alert("posting to server");
}

EDIT

Plunk.

EDIT 03/02-2014, another way would be to pass a flag from the directive to the parent controller indicating whether the form is valid or not, see how the "outer button"'s ng-disabled is hooked up in the plunk (I've updated it.)


Need Your Help

libgdx no show admob

android admob libgdx

Please tell me why do not see a banner ad.

Reversing a string in python

python

I want to find a palindrome in a string.