JSFiddle - React, Tailwind, and code Playground
by Michael Piecko
HTML
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.12/angular.min.js"></script>
<div ng-app>
<div ng-controller="formTest">
<h3>Field with an invalid state does not update the model</h3>
<ul>
<li>Watch the "Debug Model" output below</li>
<li>Type something in the field</li>
<li>Press "$setValidity('invalid', false)" button</li>
<li>Type something in the field again ("fullName" gets "undefined")</li>
<li>Press "$setValidity('invalid', true)" button</li>
<li>Type something in the field again ("fullName" is updated again)</li>
</ul>
<form name="myForm">
<input type="text" name="fullName" ng-model="customer.fullName"/>
</form>
<button ng-click="invalidateField()">$setValidity('invalid', false)</button>
<button ng-click="validateField()">$setValidity('invalid', true)</button>
<table>
<tr>
<td>
<h3>Debug Model</h3>
<pre>{{ customer | json }}</pre>
</td>
<td>
<h3>Debug Form</h3>
<pre>{{ myForm | json }}</pre>
</td>
</tr>
</table>
</div>
</div>
CSS
body {
font-family: sans-serif;
}
td {
width: 50%;
vertical-align: top;
}
.ng-invalid {
border-color: red;
}
JavaScript
function formTest($scope) {
$scope.customer = {
fullName: ''
};
$scope.invalidateField = function() {
$scope.myForm.fullName.$setValidity('invalid', false);
};
$scope.validateField = function() {
$scope.myForm.fullName.$setValidity('invalid', true);
}
};