I have some issues with data and class-binding in Angular, and neither disabling the submit button works.
checkNewChampName(value) {
if (this.actualChampionships.indexOf(value) !== -1) {
this.validNewChampName = false;
} else {
this.validNewChampName = true;
}
}
<form #userForm="ngForm">
<div class="form-group">
<label>Name</label>
<input #name="ngModel" type="text" [class.is-invalid]="!validNewChampName" class="form-control" name="name" [(ngModel)]="newChampName" (keydown)="checkNewChampName(name.value)">
</div>
<button [disabled]="userForm.form.invalid" class="btn btn-primary">Submit</button>
</form>
So I would like an input, which calls a validation method for every keydown. This validation should set a class variable to true or false, depending on is the value element of an array or not. And I want to bind this boolean variable to the 'is-invalid' class of the input. I have 3 problems:
- At the beginning my input is red, and if I write something and delete to blank again, it won't be red again (it shouldn't be at the start neither), so why is it red at start?
- The validation method almost works, but it lates one character. So if 'asdf' is in the array, my input remains blue, but it changes to red to the next keydown.
- Why the button is not disabled when my input is red?