Ik probeer een formulierveld toe te voegen met aangepaste invoerbesturing voor telefoonnummers. Ik heb het voorbeeld van de telefoon gebruikt van https://material.angular.io/components/form -veld/voorbeelden.
Hier is de code:
<mat-form-field>
<example-tel-input placeholder="Phone number" required></example-tel-input>
<mat-icon matSuffix>phone</mat-icon>
<mat-hint>Include area code</mat-hint>
</mat-form-field>
import {FocusMonitor} from '@angular/cdk/a11y';
import {coerceBooleanProperty} from '@angular/cdk/coercion';
import {Component, ElementRef, Input, OnDestroy} from '@angular/core';
import {FormBuilder, FormGroup} from '@angular/forms';
import {MatFormFieldControl} from '@angular/material';
import {Subject} from 'rxjs';
/** @title Form field with custom telephone number input control. */
@Component({
selector: 'form-field-custom-control-example',
templateUrl: 'form-field-custom-control-example.html',
styleUrls: ['form-field-custom-control-example.css'],
})
export class FormFieldCustomControlExample {}
/** Data structure for holding telephone number. */
export class MyTel {
constructor(public area: string, public exchange: string, public subscriber: string) {}
}
/** Custom `MatFormFieldControl` for telephone number input. */
@Component({
selector: 'example-tel-input',
templateUrl: 'example-tel-input-example.html',
styleUrls: ['example-tel-input-example.css'],
providers: [{provide: MatFormFieldControl, useExisting: MyTelInput}],
host: {
'[class.example-floating]': 'shouldLabelFloat',
'[id]': 'id',
'[attr.aria-describedby]': 'describedBy',
}
})
export class MyTelInput implements MatFormFieldControl<MyTel>, OnDestroy {
static nextId = 0;
parts: FormGroup;
stateChanges = new Subject<void>();
focused = false;
ngControl = null;
errorState = false;
controlType = 'example-tel-input';
id = `example-tel-input-${MyTelInput.nextId++}`;
describedBy = '';
get empty() {
const {value: {area, exchange, subscriber}} = this.parts;
return !area && !exchange && !subscriber;
}
get shouldLabelFloat() { return this.focused || !this.empty; }
@Input()
get placeholder(): string { return this._placeholder; }
set placeholder(value: string) {
this._placeholder = value;
this.stateChanges.next();
}
private _placeholder: string;
@Input()
get required(): boolean { return this._required; }
set required(value: boolean) {
this._required = coerceBooleanProperty(value);
this.stateChanges.next();
}
private _required = false;
@Input()
get disabled(): boolean { return this._disabled; }
set disabled(value: boolean) {
this._disabled = coerceBooleanProperty(value);
this.stateChanges.next();
}
private _disabled = false;
@Input()
get value(): MyTel | null {
const {value: {area, exchange, subscriber}} = this.parts;
if (area.length === 3 && exchange.length === 3 && subscriber.length === 4) {
return new MyTel(area, exchange, subscriber);
}
return null;
}
set value(tel: MyTel | null) {
const {area, exchange, subscriber} = tel || new MyTel('', '', '');
this.parts.setValue({area, exchange, subscriber});
this.stateChanges.next();
}
constructor(fb: FormBuilder, private fm: FocusMonitor, private elRef: ElementRef<HTMLElement>) {
this.parts = fb.group({
area: '',
exchange: '',
subscriber: '',
});
fm.monitor(elRef, true).subscribe(origin => {
this.focused = !!origin;
this.stateChanges.next();
});
}
ngOnDestroy() {
this.stateChanges.complete();
this.fm.stopMonitoring(this.elRef);
}
setDescribedByIds(ids: string[]) {
this.describedBy = ids.join(' ');
}
onContainerClick(event: MouseEvent) {
if ((event.target as Element).tagName.toLowerCase() != 'input') {
this.elRef.nativeElement.querySelector('input')!.focus();
}
}
}
voorbeeld-tel-input-voorbeeld.html
<div [formGroup]="parts" class="example-tel-input-container">
<input class="example-tel-input-element" formControlName="area" size="3">
<span class="example-tel-input-spacer">–</span>
<input class="example-tel-input-element" formControlName="exchange" size="3">
<span class="example-tel-input-spacer">–</span>
<input class="example-tel-input-element" formControlName="subscriber" size="4">
</div>
Maar ik krijg de volgende foutmelding:
ERROR Error: mat-form-field moet een MatFormFieldControl bevatten.
Antwoord 1, autoriteit 100%
Moet import
twee modules en voeg deze toe in de import- en exportsectie.
import { MatFormFieldModule } from '@angular/material/form-field';
import { MatInputModule } from '@angular/material/input';
@NgModule ({
imports: [ MatFormFieldModule, MatInputModule ],
exports: [ MatFormFieldModule, MatInputModule ]
})
Antwoord 2, autoriteit 50%
Zorg ervoor dat MatInputModuleis geïmporteerd en dat <mat-form-field>
<input>
bevat met matInput / matSelect
richtlijnen.
https://github.com/angular/material2/issues/7898
Antwoord 3, autoriteit 25%
Importeer MatInputModule
, mijn fout opgelost
Antwoord 4, autoriteit 11%
Je moet je klas specificeren als provider voor MatFormFieldControl
@Component({
selector: 'form-field-custom-control-example',
templateUrl: 'form-field-custom-control-example.html',
styleUrls: ['form-field-custom-control-example.css'],
providers: [
{ provide: MatFormFieldControl, useExisting: FormFieldCustomControlExample }
]
})
Antwoord 5, autoriteit 9%
Vergeet ook niet het name-attribuut in de invoertag te schrijven:
name="yourName"
Antwoord 6, autoriteit 7%
Een ander mogelijk probleem bij het sluiten van de invoertag. Als u code kopieert uit een van de Angular-voorbeelden (https://material.angular.io/components /datepicker/overview), kunt u eindigen met de code:
<mat-form-field appearance="fill">
<mat-label>Choose a date</mat-label>
<input matInput [matDatepicker]="picker">
<mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
<mat-datepicker #picker></mat-datepicker>
</mat-form-field>
De invoer moet de tag close (slash) hebben:
<input matInput [matDatepicker]="picker" />
Antwoord 7, autoriteit 2%
-
als u een ‘input’-tag in uw code gebruikt, samen met ‘mat-form-field’, zorg er dan voor dat u ‘matInput’ opneemt in de invoertag
-
als er een *ngIf aanwezig is in de onderliggende tag van ‘mat-form-field’, specificeer dan de *ngIf-voorwaarde in de tag ‘mat-form-field’
Antwoord 8
Fout zegt dat mat-form-field
ten minste één formulierveld moet bevatten waaruit invoer wordt gehaald.
Bijv: matInput
mat-select
enz.
In jouw geval mag je de tag matInput
toevoegen aan je invoervelden in example-tel-input-example.html. En je zou ook mat-form-field
binnen de example-tel-input-example
component kunnen verplaatsen en deze toevoegen aan elk matInput
veld.
<div [formGroup]="parts" class="example-tel-input-container">
<mat-form-field>
<input matInput class="example-tel-input-element" formControlName="area" size="3">
</mat-form-field>
<span class="example-tel-input-spacer">–</span>
<mat-form-field>
<input matInput class="example-tel-input-element" formControlName="exchange" size="3">
</mat-form-field>
<span class="example-tel-input-spacer">–</span>
<mat-form-field>
<input matInput class="example-tel-input-element" formControlName="subscriber" size="4">
</mat-form-field>
</div>
Opmerking : mat-icon
of mat-hint
kan niet worden beschouwd als formuliervelden