import {Component, OnInit} from '@angular/core';
import {FormBuilder, FormControl, FormGroup, Validators} from '@angular/forms';
import {Observable} from 'rxjs';
import {startWith, map} from 'rxjs/operators';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent {
title = 'Test';
answers: any = [
{value: '0', viewValue: 'Yes'},
{value: '1', viewValue: 'No'}
];
customerdataForm = new FormGroup({
answerControl: new FormControl("", Validators.required),
sapNumberControl: new FormControl({value: "", disabled: true}, [
Validators.required
])
});
onSelectionChanged({ value } ) {
console.log(value);
if (value === '0') {
console.log("true");
this.customerdataForm.get('sapNumberControl')?.disable;
} else {
console.log("false");
this.customerdataForm.get('sapNumberControl')?.enable();
}
}
}
export class InputForm{}
In this code im getting the error
Error: src/app/app.component.ts:29:24 - error TS7031: Binding element 'value' implicitly has an 'any' type.
This code is also from a Stackoverflow Discussion
Where it also works in a StackBlitz
Can anyone help?
I dont know where this error comes from and i tried now many things, nothin worked
EDIT:
Trying to implement the same functionality as described in the mentioned stackoverflow discussion
Used html
<form [formGroup]="customerdataForm">
<mat-form-field>
<mat-label>Select an option</mat-label>
<mat-select formControlName="answerControl" (selectionChange)="onSelectionChanged($event)">
<!-- <mat-option>--</mat-option> -->
<mat-option *ngFor="let answer of answers" [value]="answer">
{{answer.viewValue}}
</mat-option>
</mat-select>
</mat-form-field>
<p>Customer SAP (only if they are an existing customer)</p>
<mat-form-field>
<input matInput placeholder="Customer_SAP" formControlName="sapNumberControl">
</mat-form-field>
</form>
CodePudding user response:
I think the error is in the function onSelectionChanged({ value } ) {}
try adding a type to value
If you know the type of what you are getting, I think its a string so try with string or any
onSelectionChanged(value: string ) { }
You need to specify the types of the variables you are using in TS as its a strongly typed language.
Edit: also take out the { }
from value