3

Hi I have a form with some inputs like email, title, color...

I have 2 buttons "Save" and "Undo changes".

I need that this buttons will be always disabled accept, if something was changed. For example I started to change email and then I considered. While I'm typing new email buttons should be enabled, but after click on one of them they should be disabled again.

<form name="form" ng-submit="change()" novalidate>
    <input type="text" ng-model="title"/>
    <input type="email" name="email" ng-model="email" placeholder="{{email}}" />
    <input type="color" ng-model="color"/>

    <button type="submit" ng-disabled="!form.$dirty">SAVE</button>
    <a ng-click="cancel()" ng-disabled="!form.$dirty">UNDO CHANGES</a>
</form>

How can I make them disable again after click on one of buttons?

2 Answers 2

1

Call $scope.form.$setPristine() to cancel all the dirty flags:

function TodoCtrl($scope) {

    $scope.change = function() {
        $scope.form.$setPristine();
    }

    $scope.cancel = function() {
        $scope.form.$setPristine();
    }
}

Example: http://jsfiddle.net/ej5et0f5/1/

Sign up to request clarification or add additional context in comments.

Comments

1

Simply create a flag variable that you'll set to true after submitting and to false after changing inputs, then just do:

<button type="submit" ng-disabled="flagVariable">

Comments

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.