简体   繁体   中英

matInput Label Hiding on focus

I just started using Angular Material. I copied a code from Official Material Input docs.

But, whenever I focus on the input, it hides when it floats.

Before Focus

After Focus

PS : It happens on every form field and input. Please Help Me.

Thank You

EDIT:

I have created a material module in which I import all the necessary modules related to material and then export it to app.module.ts

material.module.ts

...
import { MatFormFieldModule } from '@angular/material/form-field';
import { MatInputModule } from '@angular/material/input';
...

const MaterialComponents = [
    ...
    MatFormFieldModule,
    MatInputModule,
    ...
];

@NgModule({
    imports: [ MaterialComponents ],
    exports : [ MaterialComponents ]
})

app.module.ts

...
import { MaterialModule } from './material/material.module';
import { FlexLayoutModule, MediaChange, MediaObserver } from "@angular/flex-layout";

@NgModule({
    declarations: [
        ...
        routingComponents,
    ],
    imports: [
        ...
        FormsModule,
        ReactiveFormsModule,
        ...

        /* Material Imports */
        MaterialModule,
        FlexLayoutModule,
        NgbModule,
    ],
    providers: [ Title ],
    bootstrap: [ AppComponent ]
})

home.component.html

<form class="form">
        <mat-form-field class="example-full-width" appearance="outline">
            <mat-label>Email</mat-label>
            <input matInput [formControl]="emailFormControl" [errorStateMatcher]="matcher" placeholder="Ex. pat@example.com">
            <!-- <mat-hint>Errors appear instantly!</mat-hint> -->
            <mat-error *ngIf="emailFormControl.hasError('email') && !emailFormControl.hasError('required')">
                Please enter a valid email address
            </mat-error>
            <mat-error *ngIf="emailFormControl.hasError('required')">
                Email is <strong>required</strong>
            </mat-error>
        </mat-form-field>

        <button mat-raised-button color="accent">Save</button>
    </form>

It's just copied from Official Material Docs

Node version is @12.16.2

Angular version is @9.1.3

Turns out. I wrote this due to horizontal overflow in toolbar

styles.css


* {
    overflow-x: hidden;
}

The Issue was solved after I removed the css property.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM