Angular Material表单 – 添加一个mat-checkbox会破坏样式表。

huangapple go评论64阅读模式
英文:

Angular Material form - adding a mat-checkbox ruins the stylesheet

问题

I am having the hardest time adding a simple mat-checkbox to my Angular Material form. I created a sample project where the same problem reproduces.

<form>
  <mat-form-field>
    <mat-label>Some label</mat-label>
    <input type="number" matInput [formControl]="companyNameFormControl">
  </mat-form-field>
  <!-- <mat-form-field>
    <mat-label>Another label</mat-label>
    <mat-checkbox type="checkbox"></mat-checkbox>
  </mat-form-field> -->
</form>

看起来像:

Angular Material表单 – 添加一个mat-checkbox会破坏样式表。

When I uncommented the checkbox:

<form>
  <mat-form-field>
    <mat-label>Some label</mat-label>
    <input type="number" matInput [formControl]="companyNameFormControl">
  </mat-form-field>
  <mat-form-field>
    <mat-label>Another label</mat-label>
    <mat-checkbox type="checkbox"></mat-checkbox>
  </mat-form-field>
</form>

然后页面呈现如下:

Angular Material表单 – 添加一个mat-checkbox会破坏样式表。

I have been googling for hours, but nothing seems to solve the problem.

For completeness the app module and component

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

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';

import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { MatFormFieldModule } from '@angular/material/form-field';
import { MatInputModule } from '@angular/material/input';
import { MatCheckboxModule } from '@angular/material/checkbox';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    FormsModule,
    ReactiveFormsModule,
    AppRoutingModule,
    BrowserAnimationsModule,
    MatFormFieldModule,
    MatInputModule,
    MatCheckboxModule,
    
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }
import { Component } from '@angular/core';
import { FormControl } from '@angular/forms';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'angular-tour-of-heroes';
  companyNameFormControl = new FormControl('');
}
英文:

I am having the hardest time adding a simple mat-checkbox to my Angular Material form. I created a sample project where the same problem reproduces.

&lt;form&gt;
  &lt;mat-form-field&gt;
    &lt;mat-label&gt;Some label&lt;/mat-label&gt;
    &lt;input type=&quot;number&quot; matInput [formControl]=&quot;companyNameFormControl&quot;&gt;
  &lt;/mat-form-field&gt;
  &lt;!-- &lt;mat-form-field&gt;
    &lt;mat-label&gt;Another label&lt;/mat-label&gt;
    &lt;mat-checkbox type=&quot;checkbox&quot;&gt;&lt;/mat-checkbox&gt;
  &lt;/mat-form-field&gt; --&gt;
&lt;/form&gt;

Looks like:

Angular Material表单 – 添加一个mat-checkbox会破坏样式表。

When I uncommented the checkbox:

&lt;form&gt;
  &lt;mat-form-field&gt;
    &lt;mat-label&gt;Some label&lt;/mat-label&gt;
    &lt;input type=&quot;number&quot; matInput [formControl]=&quot;companyNameFormControl&quot;&gt;
  &lt;/mat-form-field&gt;
  &lt;mat-form-field&gt;
    &lt;mat-label&gt;Another label&lt;/mat-label&gt;
    &lt;mat-checkbox type=&quot;checkbox&quot;&gt;&lt;/mat-checkbox&gt;
  &lt;/mat-form-field&gt;
&lt;/form&gt;

Then the page renders like this:

Angular Material表单 – 添加一个mat-checkbox会破坏样式表。

I have been googling for hours, but nothing seems to solve the problem.

For completeness the app module and component

import { NgModule } from &#39;@angular/core&#39;;
import { BrowserModule } from &#39;@angular/platform-browser&#39;;

import { AppRoutingModule } from &#39;./app-routing.module&#39;;
import { AppComponent } from &#39;./app.component&#39;;
import { BrowserAnimationsModule } from &#39;@angular/platform-browser/animations&#39;;

import {FormsModule, ReactiveFormsModule} from &#39;@angular/forms&#39;;
import { MatFormFieldModule } from &#39;@angular/material/form-field&#39;;
import { MatInputModule } from &#39;@angular/material/input&#39;;
import { MatCheckboxModule } from &#39;@angular/material/checkbox&#39;;

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    FormsModule,
    ReactiveFormsModule,
    AppRoutingModule,
    BrowserAnimationsModule,
    MatFormFieldModule,
    MatInputModule,
    MatCheckboxModule,
    
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

import { Component } from &#39;@angular/core&#39;;
import { FormControl } from &#39;@angular/forms&#39;;

@Component({
  selector: &#39;app-root&#39;,
  templateUrl: &#39;./app.component.html&#39;,
  styleUrls: [&#39;./app.component.css&#39;]
})
export class AppComponent {
  title = &#39;angular-tour-of-heroes&#39;;
  companyNameFormControl = new FormControl(&#39;&#39;);
}

答案1

得分: 2

I 在 StackBlitz 中重建了您的设置。如果您打开开发者控制台,您将看到以下错误信息:

> ERROR Error: mat-form-field must contain a MatFormFieldControl.
at getMatFormFieldMissingControlError (vendor.js:68177:10)
at MatFormField._assertFormFieldControl (vendor.js:68449:13)
at MatFormField.ngAfterContentInit (vendor.js:68352:10)
at callHook (vendor.js:31528:14)
at callHooks (vendor.js:31501:9)
at executeInitAndCheckHooks (vendor.js:31458:5)
at refreshView (vendor.js:39182:11)
at refreshComponent (vendor.js:40157:7)
at refreshChildComponents (vendor.js:38941:5)
at refreshView (vendor.js:39193:7)

这意味着您的复选框上不需要&lt;mat-form-field&gt;包装,如官方 Angular Material 文档中所述。请查看以下代码,无需 mat-form-field 包装。

&lt;section class=&quot;example-section&quot;&gt;
  &lt;mat-checkbox class=&quot;example-margin&quot;&gt;Check me!&lt;/mat-checkbox&gt;
  &lt;mat-checkbox class=&quot;example-margin&quot; [disabled]=&quot;true&quot;&gt;Disabled&lt;/mat-checkbox&gt;
&lt;/section&gt;

至于您的代码,这已足够:

&lt;form&gt;
  &lt;mat-form-field&gt;
    &lt;mat-label&gt;Some label&lt;/mat-label&gt;
    &lt;input type=&quot;number&quot; matInput [formControl]=&quot;companyNameFormControl&quot; /&gt;
  &lt;/mat-form-field&gt;
  &lt;mat-label&gt;Another label&lt;/mat-label&gt;
  &lt;mat-checkbox type=&quot;checkbox&quot;&gt;&lt;/mat-checkbox&gt;
&lt;/form&gt;

查看这个具有工作示例的 StackBlitz

英文:

I rebuilt your setup in StackBlitz. If you open up your developer's console, you will see this error:

> ERROR Error: mat-form-field must contain a MatFormFieldControl.
at getMatFormFieldMissingControlError (vendor.js:68177:10)
at MatFormField._assertFormFieldControl (vendor.js:68449:13)
at MatFormField.ngAfterContentInit (vendor.js:68352:10)
at callHook (vendor.js:31528:14)
at callHooks (vendor.js:31501:9)
at executeInitAndCheckHooks (vendor.js:31458:5)
at refreshView (vendor.js:39182:11)
at refreshComponent (vendor.js:40157:7)
at refreshChildComponents (vendor.js:38941:5)
at refreshView (vendor.js:39193:7)

This means that the &lt;mat-form-field&gt; wrapper on your checkbox is not needed, as described in the official Angular Material docs. See below for the code, no need for a mat-form-field wrapper.

&lt;section class=&quot;example-section&quot;&gt;
  &lt;mat-checkbox class=&quot;example-margin&quot;&gt;Check me!&lt;/mat-checkbox&gt;
  &lt;mat-checkbox class=&quot;example-margin&quot; [disabled]=&quot;true&quot;&gt;Disabled&lt;/mat-checkbox&gt;
&lt;/section&gt;

And for your code, it means that this is sufficient:

&lt;form&gt;
  &lt;mat-form-field&gt;
    &lt;mat-label&gt;Some label&lt;/mat-label&gt;
    &lt;input type=&quot;number&quot; matInput [formControl]=&quot;companyNameFormControl&quot; /&gt;
  &lt;/mat-form-field&gt;
  &lt;mat-label&gt;Another label&lt;/mat-label&gt;
  &lt;mat-checkbox type=&quot;checkbox&quot;&gt;&lt;/mat-checkbox&gt;
&lt;/form&gt;

See this StackBlitz with a working example.

huangapple
  • 本文由 发表于 2023年4月20日 03:11:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76058065.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定