@@ -7,27 +7,21 @@ import {HttpClient} from '@angular/common/http';
77 selector : 'reactive-forms' ,
88 template : `
99 <form [formGroup]="heroForm" novalidate>
10-
11- <div class="form-group">
12- <label for="name">Name</label>
13- <input formControlName="name" class="form-control" id="name" placeholder="Enter name">
14- </div>
15-
1610 <div class="form-group">
17- <label for="street">Street</label>
18- <input formControlName="street" class="form-control" id="street" placeholder="Enter street">
19- </div>
20-
21- <div class="form-group">
22- <label for="state">City</label>
23- <ng-select [items]="cities"
11+ <label for="state">Cities</label>
12+ <ng-select *ngIf="isCitiesControlVisible"
13+ [items]="cities"
2414 bindLabel="name"
2515 bindValue="id"
26- placeholder="Select city"
27- formControlName="city">
16+ [multiple]="true"
17+ placeholder="Select cities"
18+ formControlName="selectedCitiesIds">
2819 </ng-select>
20+ <br>
21+ <button (click)="toggleCitiesControl()" class="btn btn-sm btn-secondary">Show/Hide</button>
22+ <button (click)="clearCities()" class="btn btn-sm btn-secondary">Clear</button>
2923 </div>
30-
24+ <hr>
3125 <div class="form-group">
3226 <label for="state">Age</label>
3327 <ng-select [items]="ages"
@@ -38,7 +32,7 @@ import {HttpClient} from '@angular/common/http';
3832 <br>
3933 <button class="btn btn-secondary btn-sm" (click)="toggleAgeDisable()">Toggle disabled</button>
4034 </div>
41-
35+ <hr>
4236 <div class="form-group">
4337 <label for="album">Favorite album</label>
4438 <ng-select [items]="albums"
@@ -53,7 +47,7 @@ import {HttpClient} from '@angular/common/http';
5347 </ng-select>
5448 <small class="form-text text-muted">Albums data from backend using HttpClient.</small>
5549 </div>
56-
50+ <hr>
5751 <div class="form-group">
5852 <label for="album">Favorite photo</label>
5953 <ng-select [items]="photos"
@@ -81,10 +75,12 @@ export class ReactiveFormsComponent {
8175
8276 heroForm : FormGroup ;
8377
78+ isCitiesControlVisible = true ;
8479 cities : NgOption [ ] = [
8580 { id : 1 , name : 'Vilnius' } ,
8681 { id : 2 , name : 'Kaunas' } ,
87- { id : 3 , name : 'Pavilnys' , disabled : true }
82+ { id : 3 , name : 'Pavilnys (Disabled)' , disabled : true } ,
83+ { id : 4 , name : 'Pabradė' } ,
8884 ] ;
8985
9086 ages : NgOption [ ] = [
@@ -105,9 +101,7 @@ export class ReactiveFormsComponent {
105101 this . loadPhotos ( ) ;
106102
107103 this . heroForm = this . fb . group ( {
108- name : [ '' , Validators . required ] ,
109- street : '' ,
110- city : '' ,
104+ selectedCitiesIds : [ ] ,
111105 age : '' ,
112106 album : '' ,
113107 photo : ''
@@ -122,6 +116,14 @@ export class ReactiveFormsComponent {
122116 }
123117 }
124118
119+ toggleCitiesControl ( ) {
120+ this . isCitiesControlVisible = ! this . isCitiesControlVisible ;
121+ }
122+
123+ clearCities ( ) {
124+ this . heroForm . get ( 'selectedCitiesIds' ) . patchValue ( [ ] ) ;
125+ }
126+
125127 private loadAlbums ( ) {
126128 this . http . get < any [ ] > ( 'https://jsonplaceholder.typicode.com/albums' ) . subscribe ( rsp => {
127129 this . albums = rsp ;
0 commit comments