【发布时间】:2018-03-24 03:00:23
【问题描述】:
我们正在尝试在我们公司构建自己的表单域组件。我们正在尝试像这样包装材料设计的组件:
字段:
<mat-form-field>
<ng-content></ng-content>
<mat-hint align="start"><strong>{{hint}}</strong> </mat-hint>
<mat-hint align="end">{{message.value.length}} / 256</mat-hint>
<mat-error>This field is required</mat-error>
</mat-form-field>
文本框:
<field hint="hint">
<input matInput
[placeholder]="placeholder"
[value]="value"
(change)="onChange($event)"
(keydown)="onKeydown($event)"
(keyup)="onKeyup($event)"
(keypress)="onKeypress($event)">
</field>
用法:
<textbox value="test" hint="my hint"></textbox>
这大约会导致:
<textbox placeholder="Personnummer/samordningsnummer" value="" ng-reflect-placeholder="Personnummer/samordningsnummer">
<field>
<mat-form-field class="mat-input-container mat-form-field>
<div class="mat-input-wrapper mat-form-field-wrapper">
<div class="mat-input-flex mat-form-field-flex">
<div class="mat-input-infix mat-form-field-infix">
<input _ngcontent-c4="" class="mat-input-element mat-form-field-autofill-control" matinput="" ng-reflect-placeholder="Personnummer/samordningsnummer" ng-reflect-value="" id="mat-input-2" placeholder="Personnummer/samordningsnummer" aria-invalid="false">
<span class="mat-input-placeholder-wrapper mat-form-field-placeholder-wrapper"></span>
</div>
</div>
<div class="mat-input-underline mat-form-field-underline">
<span class="mat-input-ripple mat-form-field-ripple"></span>
</div>
<div class="mat-input-subscript-wrapper mat-form-field-subscript-wrapper"></div>
</div>
</mat-form-field>
</field>
</textbox>
但我得到 "mat-form-field must contain a MatFormFieldControl" 在控制台中。我想这与不直接包含 matInput-field 的 mat-form-field 有关。但它包含它,它只是带有 ng-content 投影。
【问题讨论】:
-
您解决了这个问题吗?我有完全相同的问题。答案不相关。
-
不,不幸的是:/。我发现了这个:github.com/angular/material2/issues/9411,如果我理解正确,这是不支持的 atm。
-
好的.. 谢谢,我最终制作了一个组件,它只包含所有提示和验证并将其放在输入元素下。
-
很抱歉,它没有用,我的问题与那个答案几乎没有关系。
-
我的情况,我在 标签中使用了 *ngIf。将 if 条件移到
解决了我的问题。
标签: angular typescript angular-material2