angular / angular/components

Misleading *ngIf attribute in <mat-error> example?

Đang mở
#18,430 4 bình luận 23 reaction 0 người được giao Xem trên GitHub
area: material/form-field docs needs: discussion P4
Ngôn ngữ chính
TypeScript
Star
25k
Fork
6.8k
Merge trung bình
1 ngày 8 giờ
Pull request đã merge (30 ngày)
91

Mô tả

#### Documentation Feedback

I was working on form validation and wanted to take advantage of `` elements. I based my code on the following example:
```



Enter your email

{{getErrorMessage()}}


```
As many, I thought at first that it was possible to use `` for its aesthetics only and that using a `FormControl` was optional. But apparently, `` shows up only if there is a `FormControl` used at some point, and if that `FormControl` is in an error state. The docs also state that:

> If a form field can have more than one error state, it is up to the consumer to toggle which messages should be displayed. This can be done with CSS, `ngIf` or `ngSwitch`.

Which means, if I got that right, that `*ngIf` is more of an **additional** condition deciding if `` is displayed and has no effect if the `FormControl` isn't already in an error state. According to my tests, this seems to be the case. If I am right, I think this line is incredibly misleading:
```
{{getErrorMessage()}}
```
Because that `*ngIf="email.invalid"` does not dictate if the `` element is displayed (something anyone sane thinks it does at first sight), nor does it have any interesting effect in this example since `email.invalid` seems to always be true if the `` element is displayed. During my tests, having `*ngIf="email.invalid"` or not did not make any difference.

Consequently, I suggest:

- that `*ngIf="email.invalid"` is removed from the aforementioned example,
- that ``'s dependency on using a `FormControl` is explicitly mentioned in the docs
- and that, maybe, another example showing how the `*ngIf` is used to handle several error messages **in the template** (instead of using `getErrorMessage()` in the component's TS file) is added.

**Affected documentation page:** https://material.angular.io/components/form-field/overview#error-messages

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Hướng nghiên cứu

Bắt đầu tại phần “Thông báo lỗi” của tổng quan về các trường biểu mẫu và xem xét ví dụ mat-error cùng phần giải thích xung quanh về các trạng thái lỗi của FormControl. Được xem là hoàn tất khi ví dụ và tài liệu giải thích chính xác vai trò của *ngIf*, đồng thời mọi ví dụ template được đề xuất đều thể hiện rõ cách xử lý nhiều thông báo.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
angular, typescript
Lĩnh vực
documentation
Loại issue
Tài liệu
Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Đặc tả rõ ràng
Mức phù hợp với người mới
52/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.