1

I know there has been similar questions about this topic but none of them worked for me since the solutions depend on either AngularJS or JQuery.

In Angular 5, I'm trying to disable the <input> of a <mat-form-field>:

test.component.html

<mat-form-field class="example-form">
  <input matInput disabled [matDatepicker]="picker" [value]="startDate || ab.value" placeholder="Gültig ab" [formControl]="ab">
  <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
  <mat-datepicker #picker disabled="false" ></mat-datepicker>
</mat-form-field>

But this doesn't work at all. I even tried to set disabled=true but this didn't get the job done. Additionally, I can't disable any of the other form fields in my component.

This is how my TypeScript file looks like:

test.component.ts

import ...

@Component({ ... })

export class TestComponent implements OnInit {
// A Couple of @Input statements
@Input() ...

// Create Formcontrollers
 controlNumber = new FormControl("", [Validators.pattern("[0-9]*")]);
 controlText = new FormControl("", [Validators.pattern("[A-Z-a-z]*")]);
 datePicker = new FormControl(moment());


 constructor(private http: HttpClient) {  }

 ngOnInit() {
 }

 ...

 getNotANumberMessage() {
  return this.controlNumber .hasError("pattern") ? "Numbers only" : "";
 }

 getNotAStringMessage() {
   return this.controlText.hasError("pattern") ? "Strings only" : "";
 }
 }

What am I missing?

Any help is highly appreciated!

1 Answer 1

2

Create a formcontroller for ab (your input) and set the disable property true.

ab = new FormControl("", [ Validators.pattern("[A-Z-a-z]*"), disabled:true ]);
Sign up to request clarification or add additional context in comments.

1 Comment

You meant this, right? ab = new FormControl({value: "", disabled: true}, Validators.pattern("[A-Za-z]*")); See the Official Documentation for further information

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.