typescript 在Angular 15中不工作的mat-form-field的输入中需要

bnl4lu3b  于 2023-03-31  发布在  TypeScript
关注(0)|答案(1)|浏览(145)

在Angular 15中不工作的mat-form-field输入中需要,但在Angular 13中工作正常。

<h2 mat-dialog-title>Add new category</h2>
<mat-dialog-content>

    <form [formGroup]="categoryForm">
        <div class="category-container">
            <mat-form-field>
                <input matInput placeholder="Name" required/>
            </mat-form-field>
        </div>
    </form>
</mat-dialog-content>

Required不适用于此包。json:

{
  "name": "front-inventory",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "watch": "ng build --watch --configuration development",
    "test": "ng test"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "^15.2.4",
    "@angular/cdk": "^15.2.2",
    "@angular/common": "^15.2.4",
    "@angular/compiler": "^15.2.4",
    "@angular/core": "^15.2.4",
    "@angular/flex-layout": "^15.0.0-beta.42",
    "@angular/forms": "^15.2.4",
    "@angular/material": "^15.2.2",
    "@angular/platform-browser": "^15.2.4",
    "@angular/platform-browser-dynamic": "^15.2.4",
    "@angular/router": "^15.2.4",
    "rxjs": "~7.8.0",
    "tslib": "^2.3.0",
    "zone.js": "~0.12.0"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "^15.2.1",
    "@angular/cli": "^15.2.4",
    "@angular/compiler-cli": "^15.2.4",
    "@types/jasmine": "~4.3.0",
    "jasmine-core": "~4.5.0",
    "karma": "~6.4.0",
    "karma-chrome-launcher": "~3.1.0",
    "karma-coverage": "~2.2.0",
    "karma-jasmine": "~5.1.0",
    "karma-jasmine-html-reporter": "~2.0.0",
    "typescript": "~4.9.4"
  }
}

但是,required可以使用angular.json:

{
  "name": "front-inventory",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "watch": "ng build --watch --configuration development",
    "test": "ng test"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "~13.0.0",
    "@angular/cdk": "^13.2.3",
    "@angular/common": "~13.0.0",
    "@angular/compiler": "~13.0.0",
    "@angular/core": "~13.0.0",
    "@angular/flex-layout": "^13.0.0-beta.38",
    "@angular/forms": "~13.0.0",
    "@angular/material": "^13.2.3",
    "@angular/platform-browser": "~13.0.0",
    "@angular/platform-browser-dynamic": "~13.0.0",
    "@angular/router": "~13.0.0",
    "rxjs": "~7.4.0",
    "tslib": "^2.3.0",
    "zone.js": "~0.11.4"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~13.0.4",
    "@angular/cli": "~13.0.4",
    "@angular/compiler-cli": "~13.0.0",
    "@types/jasmine": "~3.10.0",
    "@types/node": "^12.11.1",
    "jasmine-core": "~3.10.0",
    "karma": "~6.3.0",
    "karma-chrome-launcher": "~3.1.0",
    "karma-coverage": "~2.0.3",
    "karma-jasmine": "~4.0.0",
    "karma-jasmine-html-reporter": "~1.7.0",
    "typescript": "~4.4.3"
  }
}

有人知道为什么吗?

bakd9h0s

bakd9h0s1#

您应该在表单字段中添加<mat-label>,而不是在输入中放置占位符:

<mat-form-field>
    <mat-label>Name</mat-label>
    <input matInput required/>
</mat-form-field>

相关问题