1

I'm trying to write a small Angular app with the following functionality:

Form starts empty with placeholder text. User enters item in required textbox. Angular pushes item to collection. Reset form inputs to default.

I've got code that looks like this (jsfiddle: http://jsfiddle.net/Nn37v/1/) : HTML:

<div ng-controller="MyCtrl">
    <form name="talkForm">
        <input ng-model="newVoice" placeholder="Say something" required />
        <button ng-click="saySomething()">Say</button>
    </form>

    <ul>
        <li ng-repeat="c in conversation">{{c}}</li>
    </ul>
</div>

Javascript:

function MyCtrl($scope) {
    $scope.conversation =[];
    $scope.saySomething = function(){
        if ($scope.talkForm.$valid){
            //push to list
            $scope.conversation.push($scope.newVoice);
            $scope.newVoice='';
        }
    }
}

The problem I'm facing is when $scope.newVoice='' executes, the form is rendered invalid and I get the helpful HTML5 validation pop up to encourage me to fill in the form properly. Obviously this is not the behaviour I want - what is the correct way to do this with Angular?

2 Answers 2

2

To remove the HTML5 validation (since it will only work on compliant browsers) why not add the novalidate attribute to the form?

The validity of the form will still be invalid due to the 'required' attributed.

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

Comments

0

Seems that the following pull request in 1.1.x branch of Angular will add a $setPristine method to allow this to happen

https://github.com/angular/angular.js/pull/1127

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.