Javascript Angular2-TypeScript:向FormControl添加数组
我有使用Angular 2的验证表单,希望将Javascript Angular2-TypeScript:向FormControl添加数组,javascript,angular,typescript,Javascript,Angular,Typescript,我有使用Angular 2的验证表单,希望将ng2 select添加到其中 这是我的代码,Angular&HTML submit.component.ts . . . private city = new FormControl("", Validators.required); . . . ngOnInit() { this.getMelks(); this.addPropertyForm = this.formBuilder.group({
ng2 select
添加到其中
这是我的代码,Angular&HTML
submit.component.ts
. . .
private city = new FormControl("", Validators.required);
. . .
ngOnInit() {
this.getMelks();
this.addPropertyForm = this.formBuilder.group({
. . .
city: this.city,
. . .
});
}
submit.component.html
<div class="form-group">
<input class="form-control" type="text" name="city" [(ngModel)]="melk.city" placeholder="city" min="0" required>
</div>
HTML
:
<label>City</label>
<ng-select [allowClear]="false"
[items]="city"
[disabled]="disabled"
(data)="refreshValue($event)"
(selected)="selected($event)"
(removed)="removed($event)"
(typed)="typed($event)"
placeholder="Choose the city">
</ng-select>
城市
现在,如何将ng2 select添加到我的
输入和表单控件中 试试这个,希望它与for formBuilder兼容,并允许您获取当前值:
<form class="form-group" [formGroup]="cityForm">
<input class="form-control"formControlName="city" type="text" name="city" [(ngModel)]="melk.city" placeholder="city" min="0" required>
<label>City</label>
<ng-select formControlName="citySelect"
[allowClear]="false"
[items]="city"
[disabled]="disabled"
(data)="refreshValue($event)"
(selected)="selected($event)"
(removed)="removed($event)"
(typed)="typed($event)"
placeholder="Choose the city">
</ng-select>
</form>
城市
我使用的一种解决方法是在ng select的正下方创建一个隐藏输入,并将其与ngModel同步。e、 g
<label>City</label>
<ng-select #cityModel
[allowClear]="false"
[items]="city"
[disabled]="disabled"
(data)="refreshValue($event)"
(selected)="selected($event)"
(removed)="removed($event)"
(typed)="typed($event)"
placeholder="Choose the city">
</ng-select>
<input [hidden]="true" type="text" [(ngModel)]="cityModel" name="cityModel" [formControl]="city">
虽然如果您对表单控件执行某些操作,这并没有帮助。类似于city.disable()
,因为您将在隐藏元素上执行此操作
我还有一个公关来解决这个问题你想做什么?不清楚如何将select添加到输入中。。。
<label>City</label>
<ng-select #cityModel
[allowClear]="false"
[items]="city"
[disabled]="disabled"
(data)="refreshValue($event)"
(selected)="selected($event)"
(removed)="removed($event)"
(typed)="typed($event)"
placeholder="Choose the city">
</ng-select>
<input [hidden]="true" type="text" [(ngModel)]="cityModel" name="cityModel" [formControl]="city">
. . .
private city = new FormControl("", Validators.required);
private cityModel;
. . .
selected = (selectedCity) => {
this.cityModel = value;
this.city.markAsDirty();
}
removed = () => {
this.cityModel = null;
this.city.markAsDirty();
}