as someone relatively new to AngularJS, I have come across a really strange bug while writing a custom validation for a dropdown selection.
As you might expect, I've written this dropdown with
<select name="somename" id="someid" ng-model="foo" ng-model-options="{allowInvalid: true}" validate-input>
<option></option>
<option></option>
</select>
In this case validate-input is my custom directive. It is written like this:
directives.directive("validateInput", function () {
return {
restrict: 'A',
require: 'ngModel',
link: function (scope, ele, attrs, ctrl) {
ctrl.$validators.isAcceptable = function (value) {
var status = false;
if (value === '0') {
status = true;
}
return status;
};
}
}
});
Where, in this case isAcceptable is the flag I'm creating.
Anyways, to the actual question itself. Anytime I start the application, I find that the actual value of the ng-model is 'wiped'. That is, it'll either be unknown or NaN (depending on the input of course). To remedy this I put the following directive in the select tag : ng-model-options="{allowInvalid: true} However, this did not solve my problem.
The reason that this problem is so significant is that when ng-model is wiped by a false validation (set to NaN or unknown) this causes significant errors later down the road in my application. The only solution I can come to is to make ng-model="0" or ="" (this is a false validation) and keep those values instead of wiping the model itself from the application.
Aucun commentaire:
Enregistrer un commentaire