css Angular 选择选项未按预期显示

qcuzuvrc  于 2023-10-21  发布在  Angular
关注(0)|答案(1)|浏览(138)

我想有选择选项输入如图所示。

相反,我将此作为输出。我怀疑两个CSS库可能以某种方式重叠。我没有使用任何外部CSS样式。我在Angular App中使用Bootstrap,Angular Material和Angular HTML标签。

来自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": []
          }
        }
      }
    }
  }
}

浏览器控制台

<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>

编辑:事实证明,我所指的第一张图片使用了一些有Angular 的主题,可能来自Atrio。所以,即使我复制了部分代码,也是不够的。

j1dl9f46

j1dl9f461#

我觉得把bootstrap和angular材质混在一起会让你的生活真的很艰难。。但我可以看到一些问题,以解决在您的布局,这与此无关的组合。
首先,mat-label标签需要位于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>

第二,关于如何显示垫子表单字段的标签,有一个few options in the docs
我认为你所追求的可以通过在你的主模块中添加以下内容来实现:

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

相关问题