Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular Material input fields are not styled

I want to use angular material in my project.
I've added @import '~@angular/material/prebuilt-themes/pink-bluegrey.css'; into my style.css file and it works fine - tables, cards, toolbars are styled properly, but forms are not.
I've used code from example here, just by copy and paste, and the outcome looks like this:
enter image description here

What am I doing wrong? Here is my module (it is not main module):

import {NgModule} from '@angular/core';
import {CommonModule} from '@angular/common';
import {HttpClientModule} from '@angular/common/http';
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
import {ImageListComponent} from './image-list.component';
import {ImageListService} from './image-list.service';
import {FileSizePipe} from '../shared/file-size.pipe';
import {
  MdAutocompleteModule,
  MdButtonModule,
  MdCardModule,
  MdChipsModule,
  MdTableModule
} from '@angular/material';
import {CdkTableModule} from '@angular/cdk/table';
import {EnvironmentVariableComponent, ImageRunComponent} from './image-run.component';
import {FormsModule, ReactiveFormsModule} from '@angular/forms';
import { InputTestComponent } from './input-test.component';

@NgModule({
  imports: [
    CommonModule,
    HttpClientModule,
    BrowserAnimationsModule,
    MdButtonModule,
    MdCardModule,
    MdChipsModule,
    MdTableModule,
    CdkTableModule,
    MdAutocompleteModule,
    FormsModule,
    ReactiveFormsModule,
  ],
  declarations: [ImageListComponent, FileSizePipe, ImageRunComponent, EnvironmentVariableComponent, InputTestComponent],
  providers: [ImageListService],
  exports: [ImageListComponent, ImageRunComponent, InputTestComponent]
})
export class ImageModule {
}

Here is my main module:

import {BrowserModule} from '@angular/platform-browser';
import {NgModule} from '@angular/core';

import {AppComponent} from './app.component';
import {ImageModule} from './image/image.module';
import {MdToolbarModule} from "@angular/material";
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
import {FormsModule, ReactiveFormsModule} from '@angular/forms';


@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    ImageModule,
    BrowserAnimationsModule,
    MdToolbarModule,
    FormsModule,
    ReactiveFormsModule
  ],
  bootstrap: [AppComponent]
})
export class AppModule {
}

I don't understand what am I doing wrong. My forms are not looking and behaving as it supposed.

like image 449
Djent Avatar asked Sep 16 '17 13:09

Djent


2 Answers

You need to import input and form-field modules in your module imports:

import {
    MdInputModule,
    MdFormFieldModule
} from '@angular/material';

// ....
imports: [
    MdInputModule,
    MdFormFieldModule,
],
like image 199
Faisal Avatar answered Oct 09 '22 07:10

Faisal


This can also happen because you didn't wrap your <input matInput> with a <mat-form-field>.

like image 25
Kevin Beal Avatar answered Oct 09 '22 08:10

Kevin Beal