angular - 在组件中绑定 formControlName | 可选用法
问题描述
如何将formControlName
子组件中的 a 设置为可选?我想在没有 FormGroup 的 ComponentB 中使用 ComponentA,但也想在其他带有 Formgroup 的组件中使用 ComponentA。有没有可能?
组分A
<app-quantity-counter
[value]="cartItem.amount"
[step]="1"
[min]="1"
[max]="1000"
(onCounterChange)="onQuantityChange($event, cartItem)"
controlName="amount"
></app-quantity-counter>
组件A_TS
viewProviders: [
{
provide: ControlContainer,
useExisting: FormGroupDirective,
},
],
export class QuantityCounterComponent implements OnInit {
@Input() controlName: string = null;
....
组分B
<div class="quantity-counter" [class.sm]="small">
<!-- <button type="button" (click)="decrement()"><mat-icon class="text-muted">indeterminate_check_box</mat-icon></button> -->
<button type="button" (click)="decrement()">
<mat-icon class="text-muted">keyboard_arrow_down</mat-icon>
</button>
<input
type="text"
[value]="value"
[(ngModel)]="value"
(blur)="onBlur()"
(keypress)="keyPress($event)"
[formControlName]="controlName"
/>
<button type="button" (click)="increment()">
<mat-icon class="text-muted">keyboard_arrow_up</mat-icon>
</button>
<!-- <button type="button" (click)="increment()"><mat-icon class="text-muted">add_box</mat-icon></button> -->
</div>
解决方案
推荐阅读
- android - NoClassDefFoundError:解析失败:View::OnUnhandledKeyEventListener
- javascript - 如何添加通配符进行过滤
- sql - 更新主键时更新外键
- javascript - Highchart StockChart - 更新最后点值
- javascript - 如何在 Vue.js 中添加不同的字体真棒图标
- php - 搞砸了.htaccess
- office-js - office-js - 如何从 ClientResult 获取 HtmlElement
- react-native - 如何根据 react-native 中的电话号码获取所需的联系人显示名称
- python - Django Channels 与 Web 套接字的连接
- dart - 制作具有本地化需求的 Dart 包