1

I am using ng-disabled in submit button of the form. When I fill in the field, the inputForm.$valid should be true and the ng-enable should get this true value, thereby enabling the button.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head >
    <title></title>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
</head>
<body ng-app="">

    <form name="inputForm" >
        <div>
            <label>Name</label>
            <input type="text" name="name" required />
        </div>
        <div>
            <label>Email</label>
            <input type="text" name="email" required/>
        </div>

        <button type="submit" ng-disabled="{{inputForm.$valid}}">
            Submit
        </button>
    </form>

    {{inputForm.$valid}}
    {{inputForm.$invalid}}
    {{inputForm.$dirty}}
    {{inputForm.$pristine}}
</body>
</html>

but the button is not getting enabled even when the fields are typed in. what am i doing wrong?

2 Answers 2

2

You should be using:

ng-disabled="inputForm.$invalid"

in order to disable the form whenever the form is invalid. Note that you don't place the invalid property in brackets.

Full <form> code:

<form name="inputForm" >
    <div>
        <label>Name</label>
        <input type="text" name="name" required />
    </div>
    <div>
        <label>Email</label>
        <input type="text" name="email" required/>
    </div>

    <button type="submit" ng-disabled="inputForm.$invalid">
        Submit
    </button>
</form>
Sign up to request clarification or add additional context in comments.

2 Comments

What if i have two conditions ng-disabled="inputForm.$invalid && !myController.input" . this does not work for me. Do You have any solution for this
@PrashanthDamam You might want to open a new question in this case.
0

Change ng-disabled="inputForm.$valid to ng-disabled="inputForm.$invalid or ng-disabled="!inputForm.$valid, you are disabling the button when the form is valid

1 Comment

that wanted to disable the button when the form is valid. this was just a mock to understand the working

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.