Search code examples
cssangularbootstrap-4angular-materialbootstrap-5

Angular Select option is not displaying as expected


I would like to have the Select-Option Input as shown in the image. Expected Output:

Instead, I am getting this as an output. I suspect that two CSS libraries may have overlapped somehow. I haven't used any external CSS styles. I am using Bootstrap, Angular Material and Angular HTML tags in Angular App.

Actual Output:

Snippet from HTML

<div class="col-lg-4 col-md-4 col-sm-4 col-xs-8">
    <div class="form-group">
        <mat-label for="country_ref_id">Country Name<em style="color: red;">*</em></mat-label>
        <mat-form-field class="example-full-width" appearance="outline">
            <mat-select class="form-select" data-style="btn-primary" formControlName="country_ref_id" name="country_ref_id" id="country_ref_id">
                <mat-option value="">Select Country</mat-option>
                <mat-option [value]="Country.id" *ngFor="let Country of CountryList">{{Country.country}}</mat-option>
            </mat-select>
        </mat-form-field>
    </div>
</div>

angular.json

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "frontend": {
      "projectType": "application",
      "schematics": {},
      "root": "",
      "sourceRoot": "src",
      "prefix": "app",
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/frontend",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": [
              "zone.js"
            ],
            "tsConfig": "tsconfig.app.json",
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "./node_modules/bootstrap/dist/css/bootstrap.min.css",
              "@angular/material/prebuilt-themes/indigo-pink.css"
            ],
            "scripts": [
              "./node_modules/bootstrap/dist/js/bootstrap.bundle.min.js"
            ]
          },
          "configurations": {
            "production": {
              "budgets": [
                {
                  "type": "initial",
                  "maximumWarning": "500kb",
                  "maximumError": "1mb"
                },
                {
                  "type": "anyComponentStyle",
                  "maximumWarning": "2kb",
                  "maximumError": "4kb"
                }
              ],
              "outputHashing": "all"
            },
            "development": {
              "buildOptimizer": false,
              "optimization": false,
              "vendorChunk": true,
              "extractLicenses": false,
              "sourceMap": true,
              "namedChunks": true
            }
          },
          "defaultConfiguration": "production"
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "configurations": {
            "production": {
              "browserTarget": "frontend:build:production"
            },
            "development": {
              "browserTarget": "frontend:build:development"
            }
          },
          "defaultConfiguration": "development"
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "frontend:build"
          }
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "polyfills": [
              "zone.js",
              "zone.js/testing"
            ],
            "tsConfig": "tsconfig.spec.json",
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "src/styles.css"
            ],
            "scripts": []
          }
        }
      }
    }
  }
}

Browser Console

<div class="mat-mdc-form-field-infix ng-tns-c6-0"><!--bindings={
  "ng-reflect-ng-if": "false"
}--><mat-select _ngcontent-iwn-c9="" role="combobox" aria-autocomplete="none" aria-haspopup="listbox" data-style="btn-primary" formcontrolname="country_ref_id" name="country_ref_id" id="country_ref_id" class="mat-mdc-select form-select ng-tns-c8-1 ng-tns-c6-0 mat-mdc-select-required ng-pristine ng-invalid ng-star-inserted mat-mdc-select-invalid ng-touched" ng-reflect-id="country_ref_id" ng-reflect-name="country_ref_id" aria-labelledby="mat-select-value-1" tabindex="0" aria-expanded="false" aria-required="true" aria-disabled="false" aria-invalid="true"><div cdk-overlay-origin="" class="mat-mdc-select-trigger ng-tns-c8-1"><div class="mat-mdc-select-value ng-tns-c8-1" ng-reflect-ng-switch="false" id="mat-select-value-1"><!--bindings={
  "ng-reflect-ng-switch-case": "true"
}--><span class="mat-mdc-select-value-text ng-tns-c8-1 ng-star-inserted" ng-reflect-ng-switch="false"><span class="mat-mdc-select-min-line ng-tns-c8-1 ng-star-inserted">Select Country</span><!--container--><!--bindings={
  "ng-reflect-ng-switch-case": "true"
}--></span><!--bindings={
  "ng-reflect-ng-switch-case": "false"
}--></div><div class="mat-mdc-select-arrow-wrapper ng-tns-c8-1"><div class="mat-mdc-select-arrow ng-tns-c8-1"><svg viewBox="0 0 24 24" width="24px" height="24px" focusable="false" class="ng-tns-c8-1"><path d="M7 10l5 5 5-5z" class="ng-tns-c8-1"></path></svg></div></div></div><!--bindings={
  "ng-reflect-open": "false"
}--></mat-select></div>

Edit: It turns out that the first image I was referring to was using some angular theme, probably from Atrio. So, even if I copied some part of that code, it wasn't enough.


Solution

  • I think mixing bootstrap and angular materials will make your life really hard... but I can see some issues to fix in your layout which had nothing to do with this mix.

    first the mat-label tag needs to be inside the mat-form-field

    <mat-form-field class="example-full-width" appearance="outline">
      <mat-label for="country_ref_id">Country Name<em style="color: red;">*</em></mat-label>
      <mat-select class="form-select" data-style="btn-primary" formControlName="country_ref_id" name="country_ref_id" id="country_ref_id">
        <mat-option value="">Select Country</mat-option>
        <mat-option [value]="Country.id" *ngFor="let Country of CountryList">{{Country.country}}</mat-option>
      </mat-select>
    </mat-form-field>
    

    Second there are a few options in the docs as to how the label of a mat-form-field is to be displayed.

    What I think you are after can be achieved with adding the following to your main module:

    @NgModule({
      ....
      providers: [
        ...
        {provide: MAT_FORM_FIELD_DEFAULT_OPTIONS, useValue: {floatLabel: 'always'}}
      ]
    })
    ...