{"id":127766,"date":"2024-04-25T12:18:03","date_gmt":"2024-04-25T04:18:03","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/127766.html"},"modified":"2024-04-25T12:18:06","modified_gmt":"2024-04-25T04:18:06","slug":"%e4%bd%bf%e7%94%a8angular-material%e8%ae%be%e8%ae%a1%e5%93%8d%e5%ba%94%e5%bc%8f%e8%a1%a8%e5%8d%95","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/127766.html","title":{"rendered":"\u4f7f\u7528Angular Material\u8bbe\u8ba1\u54cd\u5e94\u5f0f\u8868\u5355"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/02172546\/3f914b19-b6dd-4dd8-af69-5663028d7226.webp\" alt=\"\u4f7f\u7528Angular Material\u8bbe\u8ba1\u54cd\u5e94\u5f0f\u8868\u5355\" \/><\/p>\n<p><p>\u4f7f\u7528Angular Material\u8bbe\u8ba1\u54cd\u5e94\u5f0f\u8868\u5355\u53ef\u4ee5\u6781\u5927\u63d0\u5347\u7528\u6237\u754c\u9762\u7684\u4ea4\u4e92\u6027\u548c\u73b0\u4ee3\u611f\u3002\u9996\u5148\uff0c\u4f60\u9700\u8981\u7ed3\u5408Angular\u6846\u67b6\u7684\u54cd\u5e94\u5f0f\u8868\u5355\u7279\u6027\u4e0eAngular Material\u7ec4\u4ef6\u5e93\u5b9e\u73b0\u529f\u80fd\u5f3a\u5927\u3001\u6837\u5f0f\u4e00\u81f4\u7684\u8868\u5355\u3002<strong>\u6838\u5fc3\u8981\u70b9\u5305\u62ec\uff1a\u521b\u5efa\u8868\u5355\u63a7\u4ef6\u548c\u7ec4\u3001\u4f7f\u7528Angular Material\u7ec4\u4ef6\u5b9a\u5236\u6837\u5f0f\u3001\u5229\u7528Flex Layout\u5b9e\u73b0\u54cd\u5e94\u5f0f\u8bbe\u8ba1<\/strong>\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u901a\u8fc7\u4f7f\u7528FormGroup\u548cFormControl\u6765\u521b\u5efa\u5177\u6709\u590d\u6742\u7ed3\u6784\u548c\u6821\u9a8c\u89c4\u5219\u7684\u54cd\u5e94\u5f0f\u8868\u5355\uff0c\u5e76\u4e0eAngular Material\u7ec4\u4ef6\u5e93\u914d\u5408\u4f7f\u7528\uff0c\u6253\u9020\u4e00\u4e2a\u529f\u80fd\u5b8c\u5584\u4e14\u7528\u6237\u4f53\u9a8c\u4f18\u8d28\u7684\u8868\u5355\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u642d\u5efaANGULAR MATERIAL\u73af\u5883<\/p>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u8bbe\u8ba1\u8868\u5355\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u786e\u4fddAngular\u73af\u5883\u5df2\u5b89\u88c5Angular Material\u53ca\u5176\u4f9d\u8d56\u3002\u5728\u547d\u4ee4\u884c\u4e2d\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u53ef\u4ee5\u5feb\u901f\u5b8c\u6210\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">ng add @angular\/material<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u547d\u4ee4\u4e0d\u4ec5\u4f1a\u6dfb\u52a0\u5fc5\u8981\u7684\u5305\uff0c\u800c\u4e14\u8fd8\u4f1a\u8bbe\u7f6e\u4e00\u4e9b\u9ed8\u8ba4\u7684\u4e3b\u9898\u914d\u7f6e\uff0c\u4f60\u53ef\u4ee5\u9009\u62e9\u9884\u5148\u5b9a\u4e49\u7684\u4e3b\u9898\u6216\u8005\u81ea\u5b9a\u4e49\u4e3b\u9898\u3002<\/p>\n<\/p>\n<p><p><strong>\u8bbe\u7f6e\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u5bfc\u5165MatInputModule\u3001MatButtonModule\u7b49\u6a21\u5757\u6765\u4f7f\u7528Angular Material\u63d0\u4f9b\u7684\u5404\u79cd\u8868\u5355\u63a7\u4ef6\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u5b9a\u4e49FORMGROUP\u548cFORMCONTROL<\/p>\n<\/p>\n<p><p>\u6784\u5efa\u54cd\u5e94\u5f0f\u8868\u5355\u7684\u7b2c\u4e00\u6b65\u662f\u5728\u7ec4\u4ef6\u7c7b\u4e2d\u521b\u5efa\u4e00\u4e2aFormGroup\u5b9e\u4f8b\u3002FormGroup\u662f\u4e00\u7ec4FormControl\u5b9e\u4f8b\u7684\u5bb9\u5668\uff0c\u5b83\u7684\u4f5c\u7528\u662f\u5c06FormControl\u5b9e\u4f8b\u7ec4\u7ec7\u5728\u4e00\u8d77\u3002\u6bcf\u4e2aFormControl\u5b9e\u4f8b\u90fd\u5bf9\u5e94\u4e8e\u8868\u5355\u4e2d\u7684\u4e00\u4e2a\u5b57\u6bb5\uff0c\u4f8b\u5982\uff0c\u7528\u6237\u540d\u3001\u7535\u5b50\u90ae\u4ef6\u5730\u5740\u3001\u5bc6\u7801\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-typescript\">\/\/ \u5728\u7ec4\u4ef6\u7c7b\u4e2d\u521b\u5efaFormGroup\u5b9e\u4f8b<\/p>\n<p>import { FormBuilder, FormGroup, Validators } from &#039;@angular\/forms&#039;;<\/p>\n<p>export class MyFormComponent {<\/p>\n<p>  myForm: FormGroup;<\/p>\n<p>  constructor(private fb: FormBuilder) {<\/p>\n<p>    this.myForm = this.fb.group({<\/p>\n<p>      username: [&#039;&#039;, [Validators.required, Validators.minLength(4)]],<\/p>\n<p>      em<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>l: [&#039;&#039;, [Validators.required, Validators.email]],<\/p>\n<p>      password: [&#039;&#039;, [Validators.required, Validators.minLength(8)]]<\/p>\n<p>    });<\/p>\n<p>  }<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528FormBuilder\u6765\u521b\u5efa\u4e00\u4e2a\u5305\u542busername\u3001email\u548cpassword\u63a7\u4ef6\u7684\u8868\u5355\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528ANGULAR MATERIAL\u7ec4\u4ef6<\/p>\n<\/p>\n<p><p>\u6709\u4e86FormGroup\u540e\uff0c\u63a5\u4e0b\u6765\u8981\u505a\u7684\u662f\u5728\u6a21\u677f\u4e2d\u4f7f\u7528Angular Material\u7ec4\u4ef6\u6765\u6784\u5efa\u8868\u5355\u7684\u7528\u6237\u754c\u9762\u3002\u901a\u8fc7\u4f7f\u7528<code>&lt;mat-form-field&gt;<\/code>\u6807\u7b7e\uff0c\u53ef\u4ee5\u4e3a\u8f93\u5165\u63a7\u4ef6\u6dfb\u52a0\u6f02\u4eae\u7684Material Design\u6837\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-html\">&lt;form [formGroup]=&quot;myForm&quot; (ngSubmit)=&quot;onSubmit()&quot;&gt;<\/p>\n<p>  &lt;mat-form-field appearance=&quot;fill&quot;&gt;<\/p>\n<p>    &lt;mat-label&gt;Username&lt;\/mat-label&gt;<\/p>\n<p>    &lt;input matInput formControlName=&quot;username&quot;&gt;<\/p>\n<p>  &lt;\/mat-form-field&gt;<\/p>\n<p>  &lt;mat-form-field appearance=&quot;fill&quot;&gt;<\/p>\n<p>    &lt;mat-label&gt;Email&lt;\/mat-label&gt;<\/p>\n<p>    &lt;input matInput formControlName=&quot;email&quot;&gt;<\/p>\n<p>  &lt;\/mat-form-field&gt;<\/p>\n<p>  &lt;mat-form-field appearance=&quot;fill&quot;&gt;<\/p>\n<p>    &lt;mat-label&gt;Password&lt;\/mat-label&gt;<\/p>\n<p>    &lt;input matInput type=&quot;password&quot; formControlName=&quot;password&quot;&gt;<\/p>\n<p>  &lt;\/mat-form-field&gt;<\/p>\n<p>  &lt;button mat-raised-button color=&quot;primary&quot; type=&quot;submit&quot; [disabled]=&quot;!myForm.valid&quot;&gt;Submit&lt;\/button&gt;<\/p>\n<p>&lt;\/form&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u6bcf\u4e2a<code>&lt;mat-form-field&gt;<\/code>\u63a7\u4ef6\u90fd\u5305\u542b\u4e00\u4e2alabel\u548c\u4e00\u4e2ainput\uff0c\u4f7f\u7528matInput\u6307\u4ee4\u5c06\u5176\u6807\u8bb0\u4e3aMaterial\u8f93\u5165\u63a7\u4ef6\uff0cformControlName\u5c5e\u6027\u5c06\u8f93\u5165\u63a7\u4ef6\u4e0eFormGroup\u4e2d\u5b9a\u4e49\u7684FormControl\u5b9e\u4f8b\u7ed1\u5b9a\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u56db\u3001\u5b9e\u73b0\u54cd\u5e94\u5f0fLAYOUT<\/p>\n<\/p>\n<p><p>\u5728\u73b0\u4ee3Web\u8bbe\u8ba1\u4e2d\uff0c\u4fdd\u8bc1\u8868\u5355\u80fd\u591f\u5728\u5404\u79cd\u5c4f\u5e55\u5c3a\u5bf8\u4e0a\u90fd\u4fdd\u6301\u826f\u597d\u7684\u5e03\u5c40\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002Angular Flex-Layout\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u5e03\u5c40API\uff0c\u7528\u4e8eAngular\u9879\u76ee\uff0c\u5b83\u8ba9\u6211\u4eec\u80fd\u591f\u4f7f\u7528Flexbox\u5e03\u5c40\u6765\u6784\u5efa\u54cd\u5e94\u5f0f\u8bbe\u8ba1\u3002<\/p>\n<\/p>\n<p><p>\u8fdb\u4e00\u6b65\u5730\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528Angular Flex-Layout\u63d0\u4f9b\u7684\u6307\u4ee4\u5728\u8868\u5355\u5143\u7d20\u4e0a\u5b9a\u4e49\u5f39\u6027\u76d2\u6a21\u578b\u7684\u5c5e\u6027\uff0c\u5b9e\u73b0\u54cd\u5e94\u5f0f\u5e03\u5c40\u3002\u5bfc\u5165FlexLayoutModule\u4e4b\u540e\uff0c\u5c31\u53ef\u4ee5\u5728\u6a21\u677f\u4e2d\u4f7f\u7528\u5404\u79cdFlex-Layout\u7684\u6307\u4ee4\u4e86\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-typescript\">import { FlexLayoutModule } from &#039;@angular\/flex-layout&#039;;<\/p>\n<p>@NgModule({<\/p>\n<p>  imports: [<\/p>\n<p>    \/\/ ...<\/p>\n<p>    FlexLayoutModule<\/p>\n<p>  ],<\/p>\n<p>  \/\/ ...<\/p>\n<p>})<\/p>\n<p>export class Ap<a href=\"https:\/\/docs.pingcode.com\/blog\/project-management\/52627.html\" target=\"_blank\">PMO<\/a>dule { }<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><pre><code class=\"language-html\">&lt;div fxLayout=&quot;row&quot; fxLayoutGap=&quot;10px&quot; [fxLayout.lt-sm]=&quot;column&quot;&gt;<\/p>\n<p>  &lt;mat-form-field fxFlex&gt;<\/p>\n<p>    &lt;!--  ... --&gt;<\/p>\n<p>  &lt;\/mat-form-field&gt;<\/p>\n<p>  &lt;mat-form-field fxFlex&gt;<\/p>\n<p>    &lt;!--  ... --&gt;<\/p>\n<p>  &lt;\/mat-form-field&gt;<\/p>\n<p>  &lt;mat-form-field fxFlex&gt;<\/p>\n<p>    &lt;!--  ... --&gt;<\/p>\n<p>  &lt;\/mat-form-field&gt;<\/p>\n<p>&lt;\/div&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528\u5982<code>fxLayout<\/code>\u3001<code>fxFlex<\/code>\u548c<code>fxLayoutGap<\/code>\u7b49\u6307\u4ee4\u80fd\u591f\u5e2e\u52a9\u6211\u4eec\u4e3a\u8868\u5355\u7ec4\u4ef6\u5236\u5b9a\u5f39\u6027\u5bbd\u5ea6\u548c\u95f4\u9694\uff0c\u800c<code>[fxLayout.lt-sm]=&quot;column&quot;<\/code>\u5219\u8ba9\u6211\u4eec\u80fd\u591f\u5b9a\u4e49\u5c0f\u5c4f\u5e55\u8bbe\u5907\u4e0a\u7684\u5e03\u5c40\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u589e\u5f3a\u8868\u5355\u4ea4\u4e92\u6027\u548c\u6821\u9a8c<\/p>\n<\/p>\n<p><p>\u54cd\u5e94\u5f0f\u8868\u5355\u7684\u4e00\u4e2a\u4e3b\u8981\u7279\u6027\u662f\u80fd\u591c\u5728\u7528\u6237\u4ea4\u4e92\u8fc7\u7a0b\u4e2d\u5373\u65f6\u66f4\u65b0\u548c\u6821\u9a8c\u3002Angular Material\u914d\u5408Angular\u7684\u8868\u5355API\u80fd\u591f\u5b9e\u73b0\u5404\u79cd\u590d\u6742\u7684\u6821\u9a8c\u903b\u8f91\uff0c\u5e76\u63d0\u4f9b\u5373\u65f6\u7684\u53cd\u9988\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-typescript\">export class MyFormComponent {<\/p>\n<p>  \/\/ ...<\/p>\n<p>  get username() {<\/p>\n<p>    return this.myForm.controls.username;<\/p>\n<p>  }<\/p>\n<p>  get email() {<\/p>\n<p>    return this.myForm.controls.email;<\/p>\n<p>  }<\/p>\n<p>}<\/p>\n<p>\/\/ \u5728\u6a21\u677f\u4e2d\u5229\u7528\u8fd9\u4e9bgetter\u65b9\u6cd5\u663e\u793a\u6821\u9a8c\u9519\u8bef\u4fe1\u606f<\/p>\n<p>&lt;mat-error *ngIf=&quot;username.invalid &amp;&amp; username.touched&quot;&gt;<\/p>\n<p>  Username is required and must be at least 4 characters long.<\/p>\n<p>&lt;\/mat-error&gt;<\/p>\n<p>&lt;mat-error *ngIf=&quot;email.invalid &amp;&amp; email.touched&quot;&gt;<\/p>\n<p>  Please enter a valid email address.<\/p>\n<p>&lt;\/mat-error&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u901a\u8fc7\u4f7f\u7528<code>*ngIf<\/code>\u6307\u4ee4\u7ed3\u5408FormControl\u7684state\u5c5e\u6027\uff0c\u6211\u4eec\u80fd\u591f\u5728\u7528\u6237\u5b8c\u6210\u8f93\u5165\u540e\u663e\u793a\u9519\u8bef\u63d0\u793a\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u516d\u3001\u8868\u5355\u5bfc\u822a\u4e0e\u5206\u6b65\u8868\u5355<\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u8f83\u957f\u6216\u8f83\u590d\u6742\u7684\u8868\u5355\uff0c\u4f7f\u7528\u5206\u6b65\u8868\u5355\uff08stepper\uff09\u6765\u5f15\u5bfc\u7528\u6237\u901a\u8fc7\u4e0d\u540c\u7684\u8868\u5355\u6bb5\u843d\u901a\u5e38\u4f1a\u63d0\u4f9b\u66f4\u597d\u7684\u7528\u6237\u4f53\u9a8c\u3002Angular Material\u7684MatStepper\u7ec4\u4ef6\u63d0\u4f9b\u4e86\u7075\u6d3b\u7684\u5206\u6b65\u5bfc\u822a\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-html\">&lt;mat-horizontal-stepper [linear]=&quot;true&quot; formGroupName=&quot;myForm&quot;&gt;<\/p>\n<p>  &lt;mat-step [stepControl]=&quot;myForm.controls[&#039;firstStep&#039;]&quot;&gt;<\/p>\n<p>    &lt;form [formGroup]=&quot;firstStep&quot;&gt;<\/p>\n<p>      &lt;!-- Your first step content here --&gt;<\/p>\n<p>    &lt;\/form&gt;<\/p>\n<p>  &lt;\/mat-step&gt;<\/p>\n<p>  &lt;mat-step [stepControl]=&quot;myForm.controls[&#039;secondStep&#039;]&quot;&gt;<\/p>\n<p>    &lt;form [formGroup]=&quot;secondStep&quot;&gt;<\/p>\n<p>      &lt;!-- Your second step content here --&gt;<\/p>\n<p>    &lt;\/form&gt;<\/p>\n<p>  &lt;\/mat-step&gt;<\/p>\n<p>  &lt;!-- ... --&gt;<\/p>\n<p>&lt;\/mat-horizontal-stepper&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5c06\u8868\u5355\u5206\u89e3\u4e3a\u591a\u4e2a\u6b65\u9aa4\uff0c\u4e0d\u4ec5\u80fd\u4fc3\u4f7f\u7528\u6237\u4e13\u6ce8\u4e8e\u5f53\u524d\u7684\u8f93\u5165\u5185\u5bb9\uff0c\u4e5f\u4f7f\u5f97\u8868\u5355\u7684\u5bfc\u822a\u548c\u6d41\u7a0b\u7ba1\u7406\u66f4\u52a0\u76f4\u89c2\u3002<\/p>\n<\/p>\n<p><p>\u4e03\u3001\u96c6\u6210\u7b2c\u4e09\u65b9\u9a8c\u8bc1\u4e0e\u5b9e\u65f6\u53cd\u9988<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u5185\u5efa\u7684\u9a8c\u8bc1\u5668\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u5c06Angular Material\u8868\u5355\u4e0e\u7b2c\u4e09\u65b9\u9a8c\u8bc1\u670d\u52a1\uff08\u5982reCAPTCHA\uff09\u96c6\u6210\uff0c\u63d0\u4f9b\u989d\u5916\u7684\u5b89\u5168\u6027\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u589e\u52a0\u81ea\u5b9a\u4e49\u9a8c\u8bc1\u5668\u6765\u9a8c\u8bc1\u8868\u5355\u63d0\u4ea4\u4e4b\u524d\u7528\u6237\u7684\u8f93\u5165\u662f\u5426\u7b26\u5408\u671f\u671b\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u5b9e\u65f6\u53cd\u9988\uff0c\u53ef\u4ee5\u7ed9\u7528\u6237\u5373\u65f6\u7684\u8f93\u5165\u6307\u5bfc\uff0c\u63d0\u9ad8\u7528\u6237\u4f53\u9a8c\u3002\u4f8b\u5982\uff0c\u91c7\u7528\u5bc6\u7801\u5f3a\u5ea6\u6761\u6216\u8005\u8f93\u5165\u5efa\u8bae\u63d0\u793a\u53ef\u4ee5\u7acb\u5373\u8ba9\u7528\u6237\u4e86\u89e3\u5176\u8f93\u5165\u7684\u72b6\u6001\u3002<\/p>\n<\/p>\n<p><p>\u516b\u3001\u9ad8\u7ea7\u529f\u80fd\u4e0e\u5b9a\u5236<\/p>\n<\/p>\n<p><p>Angular Material\u63d0\u4f9b\u4e86\u975e\u5e38\u591a\u7684\u81ea\u5b9a\u4e49\u529f\u80fd\uff0c\u4f60\u53ef\u4ee5\u5229\u7528\u8fd9\u4e9b\u5de5\u5177\u8fdb\u4e00\u6b65\u63d0\u5347\u8868\u5355\u7684\u529f\u80fd\u3002\u4f8b\u5982\uff0cMatAutocomplete\u53ef\u4ee5\u81ea\u52a8\u5b8c\u6210\u7528\u6237\u7684\u8f93\u5165\uff0cMatSelect\u53ef\u4ee5\u63d0\u4f9b\u4e0b\u62c9\u9009\u9879\uff0c\u800cMatDatepicker\u53ef\u4ee5\u8ba9\u7528\u6237\u65b9\u4fbf\u5730\u9009\u62e9\u65e5\u671f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-html\">&lt;mat-form-field&gt;<\/p>\n<p>  &lt;mat-label&gt;Choose a date&lt;\/mat-label&gt;<\/p>\n<p>  &lt;input matInput [matDatepicker]=&quot;picker&quot; formControlName=&quot;date&quot;&gt;<\/p>\n<p>  &lt;mat-datepicker-toggle matSuffix [for]=&quot;picker&quot;&gt;&lt;\/mat-datepicker-toggle&gt;<\/p>\n<p>  &lt;mat-datepicker #picker&gt;&lt;\/mat-datepicker&gt;<\/p>\n<p>&lt;\/mat-form-field&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528\u8fd9\u6837\u7684\u7ec4\u4ef6\u4e0d\u4ec5\u63d0\u9ad8\u4e86\u8868\u5355\u7684\u53ef\u7528\u6027\uff0c\u4e5f\u4f7f\u5f97\u7528\u6237\u8f93\u5165\u8fc7\u7a0b\u66f4\u52a0\u6109\u60a6\u3002<\/p>\n<\/p>\n<p><p>\u4e5d\u3001\u8868\u5355\u7684\u6027\u80fd\u4f18\u5316<\/p>\n<\/p>\n<p><p>\u8bbe\u8ba1Angular Material\u54cd\u5e94\u5f0f\u8868\u5355\u65f6\uff0c\u8fd8\u9700\u5173\u6ce8\u6027\u80fd\u95ee\u9898\uff0c\u5982\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u91cd\u6e32\u67d3\u548c\u4f7f\u7528\u8868\u5355\u63a7\u4ef6\u7684onPush\u53d8\u66f4\u68c0\u6d4b\u7b56\u7565\u3002\u53ef\u4ee5\u5229\u7528Angular\u7684PurePipe\u6216\u8005MemoizedSelector\u6765\u51cf\u5c11\u8ba1\u7b97\u548c\u6e32\u67d3\u8d1f\u62c5\uff0c\u4ece\u800c\u63d0\u5347\u8868\u5355\u7684\u54cd\u5e94\u901f\u5ea6\u3002<\/p>\n<\/p>\n<p><p>\u5341\u3001\u6700\u4f73\u5b9e\u8df5\u4e0e\u53ef\u8bbf\u95ee\u6027<\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u786e\u4fdd\u8868\u5355\u5bf9\u6240\u6709\u7528\u6237\u90fd\u662f\u53ef\u8bbf\u95ee\u7684\uff0c\u9700\u9075\u5b88WCAG (Web Content Accessibility Guidelines) \u6307\u5357\uff0c\u5e76\u4f7f\u7528Angular Material\u63d0\u4f9b\u7684aria-*\u5c5e\u6027\u589e\u5f3a\u8868\u5355\u7684\u65e0\u969c\u788d\u7279\u6027\u3002<\/p>\n<\/p>\n<p><p>\u7ed3\u5408Angular Material\u8bbe\u8ba1\u4e00\u4e2a\u54cd\u5e94\u5f0f\u8868\u5355\uff0c\u4e0d\u4ec5\u6d89\u53ca\u5230\u5e03\u5c40\u548c\u5916\u89c2\u4e0a\u7684\u914d\u5408\uff0c\u4e5f\u5305\u62ec\u4e86\u8868\u5355\u903b\u8f91\u548c\u7528\u6237\u4f53\u9a8c\u65b9\u9762\u7684\u8003\u8651\u3002\u4ece\u57fa\u672c\u5e03\u5c40\u5230\u7528\u6237\u8f93\u5165\u7684\u5b9e\u65f6\u53cd\u9988\u548c\u6821\u9a8c\uff0c\u6bcf\u4e00\u6b65\u90fd\u5e94\u786e\u4fdd\u7528\u6237\u80fd\u591f\u6d41\u7545\u3001\u65e0\u969c\u788d\u5730\u5b8c\u6210\u4ed6\u4eec\u7684\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528Angular Material\u6846\u67b6\u5185\u63d0\u4f9b\u7684\u7ec4\u4ef6\u548c\u670d\u52a1\uff0c\u5f00\u53d1\u8005\u4eec\u53ef\u4ee5\u7701\u53bb\u5927\u90e8\u5206\u6837\u5f0f\u548c\u529f\u80fd\u4e0a\u7684\u5904\u7406\uff0c\u4e13\u6ce8\u4e8e\u521b\u5efa\u9ad8\u6548\u3001\u53ef\u6269\u5c55\u4ee5\u53ca\u7528\u6237\u53cb\u597d\u7684\u8868\u5355\u5e94\u7528\u4f53\u9a8c\u3002\u968f\u7740Material Design\u7684\u6301\u7eed\u66f4\u65b0\u548cAngular\u793e\u533a\u7684\u4e0d\u65ad\u8fdb\u6b65\uff0cAngular Material\u54cd\u5e94\u5f0f\u8868\u5355\u8bbe\u8ba1\u5c06\u7ee7\u7eed\u5728\u73b0\u4ee3Web\u5e94\u7528\u5f00\u53d1\u4e2d\u5360\u636e\u91cd\u8981\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>\u5982\u4f55\u5728Angular\u4e2d\u4f7f\u7528Angular Material\u6765\u8bbe\u8ba1\u54cd\u5e94\u5f0f\u8868\u5355\uff1f<\/strong><\/p>\n<p>Angular Material \u662f\u4e00\u4e2a\u7528\u4e8eAngular\u6846\u67b6\u7684UI\u7ec4\u4ef6\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u73b0\u6210\u7684UI\u7ec4\u4ef6\uff0c\u5305\u62ec\u7528\u4e8e\u8bbe\u8ba1\u54cd\u5e94\u5f0f\u8868\u5355\u7684\u7ec4\u4ef6\u3002\u8981\u5728Angular\u4e2d\u4f7f\u7528Angular Material\u6765\u8bbe\u8ba1\u54cd\u5e94\u5f0f\u8868\u5355\uff0c\u60a8\u53ef\u4ee5\u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u8fdb\u884c\u64cd\u4f5c\uff1a<\/p>\n<ol>\n<li>\n<p>\u5bfc\u5165\u548c\u5b89\u88c5Angular Material\uff1a\u5728\u60a8\u7684\u9879\u76ee\u4e2d\uff0c\u901a\u8fc7\u8fd0\u884c<code>ng add @angular\/material<\/code>\u547d\u4ee4\u6765\u5b89\u88c5\u548c\u5bfc\u5165Angular Material\u5e93\u3002<\/p>\n<\/li>\n<li>\n<p>\u5bfc\u5165\u6240\u9700\u7684Angular Material\u6a21\u5757\uff1a\u5728\u60a8\u7684\u6a21\u5757\u4e2d\uff0c\u4f7f\u7528<code>import<\/code>\u8bed\u53e5\u5bfc\u5165\u6240\u9700\u7684Angular Material\u6a21\u5757\uff0c\u5982<code>MatInputModule<\/code>\uff0c<code>MatCheckboxModule<\/code>\u7b49\u3002<\/p>\n<\/li>\n<li>\n<p>\u5728\u60a8\u7684\u8868\u5355\u4e2d\u4f7f\u7528Angular Material\u7ec4\u4ef6\uff1a\u4f7f\u7528Angular Material\u63d0\u4f9b\u7684\u7ec4\u4ef6\u6765\u6784\u5efa\u60a8\u7684\u8868\u5355\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>&lt;mat-form-field&gt;<\/code>\u6765\u5305\u88c5\u8f93\u5165\u6846\uff0c\u4f7f\u7528<code>&lt;mat-checkbox&gt;<\/code>\u6765\u6dfb\u52a0\u590d\u9009\u6846\uff0c\u4f7f\u7528<code>&lt;mat-select&gt;<\/code>\u6765\u6dfb\u52a0\u4e0b\u62c9\u9009\u62e9\u6846\u7b49\u3002<\/p>\n<\/li>\n<li>\n<p>\u5229\u7528Angular\u7684\u8868\u5355\u63a7\u5236\u5668\u529f\u80fd\uff1a\u4f7f\u7528Angular\u7684\u8868\u5355\u63a7\u5236\u5668\u6765\u5904\u7406\u8868\u5355\u7684\u6821\u9a8c\u548c\u63a7\u5236\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528Angular Material\u63d0\u4f9b\u7684<code>&lt;mat-error&gt;<\/code>\u7ec4\u4ef6\u6765\u663e\u793a\u6821\u9a8c\u9519\u8bef\u6d88\u606f\uff0c\u5e76\u4f7f\u7528<code>formGroup<\/code>\u548c<code>formControlName<\/code>\u7b49\u6307\u4ee4\u6765\u4e0e\u8868\u5355\u63a7\u5236\u5668\u8fdb\u884c\u7ed1\u5b9a\u3002<\/p>\n<\/li>\n<li>\n<p>\u54cd\u5e94\u5f0f\u5e03\u5c40\u548c\u81ea\u9002\u5e94\u8bbe\u8ba1\uff1aAngular Material\u7684\u7ec4\u4ef6\u5177\u6709\u54cd\u5e94\u5f0f\u5e03\u5c40\u529f\u80fd\uff0c\u53ef\u4ee5\u6839\u636e\u5c4f\u5e55\u5927\u5c0f\u81ea\u9002\u5e94\u8c03\u6574\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528<code>&lt;mat-grid-list&gt;<\/code>\u548c<code>&lt;mat-card&gt;<\/code>\u7b49\u7ec4\u4ef6\u6765\u521b\u5efa\u9002\u5e94\u4e0d\u540c\u5c4f\u5e55\u5927\u5c0f\u7684\u5e03\u5c40\u3002<\/p>\n<\/li>\n<\/ol>\n<p><strong>Angular Material\u6709\u54ea\u4e9b\u5e38\u7528\u7684\u8868\u5355\u7ec4\u4ef6\uff1f<\/strong><\/p>\n<p>Angular Material\u63d0\u4f9b\u4e86\u8bb8\u591a\u5e38\u7528\u7684\u8868\u5355\u7ec4\u4ef6\uff0c\u53ef\u4ee5\u5e2e\u52a9\u60a8\u8bbe\u8ba1\u54cd\u5e94\u5f0f\u7684\u8868\u5355\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684Angular Material\u8868\u5355\u7ec4\u4ef6\uff1a<\/p>\n<ol>\n<li>\n<p><code>&lt;mat-input&gt;<\/code>\uff1a\u7528\u4e8e\u521b\u5efa\u6587\u672c\u8f93\u5165\u6846\uff0c\u53ef\u4ee5\u7528\u4e8e\u8f93\u5165\u5404\u79cd\u7c7b\u578b\u7684\u6570\u636e\u3002<\/p>\n<\/li>\n<li>\n<p><code>&lt;mat-checkbox&gt;<\/code>\uff1a\u7528\u4e8e\u521b\u5efa\u590d\u9009\u6846\uff0c\u7528\u6237\u53ef\u4ee5\u9009\u62e9\u6216\u53d6\u6d88\u9009\u62e9\u590d\u9009\u6846\u3002<\/p>\n<\/li>\n<li>\n<p><code>&lt;mat-radio-button&gt;<\/code>\uff1a\u7528\u4e8e\u521b\u5efa\u5355\u9009\u6309\u94ae\uff0c\u7528\u6237\u53ef\u4ee5\u4ece\u4e00\u7ec4\u9009\u9879\u4e2d\u9009\u62e9\u4e00\u4e2a\u3002<\/p>\n<\/li>\n<li>\n<p><code>&lt;mat-select&gt;<\/code>\uff1a\u7528\u4e8e\u521b\u5efa\u4e0b\u62c9\u9009\u62e9\u6846\uff0c\u7528\u6237\u53ef\u4ee5\u4ece\u9884\u5b9a\u4e49\u7684\u9009\u9879\u4e2d\u9009\u62e9\u4e00\u4e2a\u3002<\/p>\n<\/li>\n<li>\n<p><code>&lt;mat-datepicker&gt;<\/code>\uff1a\u7528\u4e8e\u521b\u5efa\u65e5\u671f\u9009\u62e9\u5668\uff0c\u7528\u6237\u53ef\u4ee5\u9009\u62e9\u65e5\u671f\u3002<\/p>\n<\/li>\n<li>\n<p><code>&lt;mat-slider&gt;<\/code>\uff1a\u7528\u4e8e\u521b\u5efa\u6ed1\u52a8\u6761\uff0c\u7528\u6237\u53ef\u4ee5\u5728\u7ed9\u5b9a\u7684\u8303\u56f4\u5185\u9009\u62e9\u4e00\u4e2a\u6570\u503c\u3002<\/p>\n<\/li>\n<li>\n<p><code>&lt;mat-form-field&gt;<\/code>\uff1a\u7528\u4e8e\u5305\u88c5 <code>&lt;input&gt;<\/code> \u5143\u7d20\uff0c\u63d0\u4f9b\u4e00\u4e2a\u7edf\u4e00\u7684\u5916\u89c2\u548c\u4e00\u4e9b\u9644\u52a0\u7684\u529f\u80fd\uff0c\u4f8b\u5982\u9519\u8bef\u6821\u9a8c\u4fe1\u606f\u7684\u663e\u793a\u3002<\/p>\n<\/li>\n<\/ol>\n<p>\u4ee5\u4e0a\u4ec5\u662f\u4e00\u4e9b\u5e38\u7528\u7684Angular Material\u8868\u5355\u7ec4\u4ef6\uff0c\u60a8\u53ef\u4ee5\u6839\u636e\u60a8\u7684\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u7ec4\u4ef6\u6765\u8bbe\u8ba1\u54cd\u5e94\u5f0f\u8868\u5355\u3002<\/p>\n<p><strong>\u5982\u4f55\u5bf9\u4f7f\u7528Angular Material\u8bbe\u8ba1\u7684\u54cd\u5e94\u5f0f\u8868\u5355\u8fdb\u884c\u6821\u9a8c\uff1f<\/strong><\/p>\n<p>\u5728\u4f7f\u7528Angular Material\u8bbe\u8ba1\u54cd\u5e94\u5f0f\u8868\u5355\u65f6\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528Angular\u7684\u8868\u5355\u63a7\u5236\u5668\u6765\u8fdb\u884c\u6821\u9a8c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u6821\u9a8c\u65b9\u6cd5\uff1a<\/p>\n<ol>\n<li>\n<p>\u5fc5\u586b\u5b57\u6bb5\u6821\u9a8c\uff1a\u5bf9\u4e8e\u5fc5\u586b\u5b57\u6bb5\uff0c\u60a8\u53ef\u4ee5\u5728\u8868\u5355\u63a7\u5236\u5668\u4e2d\u4f7f\u7528<code>Validators.required<\/code>\u6821\u9a8c\u5668\u6765\u8fdb\u884c\u6821\u9a8c\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u5728<code>FormControl<\/code>\u5bf9\u8c61\u7684\u6784\u9020\u51fd\u6570\u4e2d\u5c06<code>Validators.required<\/code>\u4f20\u9012\u7ed9<code>Validators<\/code>\u6570\u7ec4\u3002<\/p>\n<\/li>\n<li>\n<p>\u6700\u5c0f\/\u6700\u5927\u957f\u5ea6\u6821\u9a8c\uff1a\u5982\u679c\u60a8\u9700\u8981\u5bf9\u8f93\u5165\u7684\u6587\u672c\u957f\u5ea6\u8fdb\u884c\u9650\u5236\uff0c\u53ef\u4ee5\u4f7f\u7528<code>Validators.minLength<\/code>\u548c<code>Validators.maxLength<\/code>\u6821\u9a8c\u5668\u3002\u5c06\u8fd9\u4e9b\u6821\u9a8c\u5668\u4e0e<code>FormControl<\/code>\u7684\u6784\u9020\u51fd\u6570\u4e00\u8d77\u4f7f\u7528\u3002<\/p>\n<\/li>\n<li>\n<p>\u81ea\u5b9a\u4e49\u6821\u9a8c\u5668\uff1a\u5982\u679c\u60a8\u9700\u8981\u81ea\u5b9a\u4e49\u7684\u6821\u9a8c\u89c4\u5219\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49\u6821\u9a8c\u5668\u51fd\u6570\uff0c\u5e76\u5728\u9700\u8981\u7684\u5730\u65b9\u4f7f\u7528\u3002\u81ea\u5b9a\u4e49\u6821\u9a8c\u5668\u51fd\u6570\u5fc5\u987b\u8fd4\u56de\u4e00\u4e2a\u5e26\u6709\u6821\u9a8c\u9519\u8bef\u4fe1\u606f\u7684\u5bf9\u8c61\uff0c\u5982\u679c\u6821\u9a8c\u901a\u8fc7\uff0c\u5219\u8fd4\u56de<code>null<\/code>\u3002<\/p>\n<\/li>\n<li>\n<p>\u9519\u8bef\u63d0\u793a\u4fe1\u606f\u663e\u793a\uff1a\u4f7f\u7528<code>&lt;mat-error&gt;<\/code>\u7ec4\u4ef6\u6765\u663e\u793a\u6821\u9a8c\u9519\u8bef\u4fe1\u606f\u3002\u4f7f\u7528<code>FormGroup<\/code>\u548c<code>FormControlName<\/code>\u7b49\u6307\u4ee4\u6765\u4e0e\u8868\u5355\u63a7\u5236\u5668\u8fdb\u884c\u7ed1\u5b9a\uff0c\u5e76\u5728\u9700\u8981\u65f6\u663e\u793a\u9519\u8bef\u4fe1\u606f\u3002<\/p>\n<\/li>\n<\/ol>\n<p>\u901a\u8fc7\u4f7f\u7528\u4ee5\u4e0a\u7684\u6821\u9a8c\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u5bf9\u4f7f\u7528Angular Material\u8bbe\u8ba1\u7684\u54cd\u5e94\u5f0f\u8868\u5355\u8fdb\u884c\u6821\u9a8c\uff0c\u5e76\u5728\u7528\u6237\u8f93\u5165\u4e0d\u7b26\u5408\u89c4\u5b9a\u65f6\u63d0\u4f9b\u9519\u8bef\u63d0\u793a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Angular Material\u8bbe\u8ba1\u54cd\u5e94\u5f0f\u8868\u5355\u53ef\u4ee5\u6781\u5927\u63d0\u5347\u7528\u6237\u754c\u9762\u7684\u4ea4\u4e92\u6027\u548c\u73b0\u4ee3\u611f\u3002\u9996\u5148\uff0c\u4f60\u9700\u8981\u7ed3\u5408Ang [&hellip;]","protected":false},"author":3,"featured_media":127768,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/127766"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=127766"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/127766\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/127768"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=127766"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=127766"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=127766"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}