@@ -7,27 +7,21 @@ import {HttpClient} from '@angular/common/http';
7
7
selector : 'reactive-forms' ,
8
8
template : `
9
9
<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
-
16
10
<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"
24
14
bindLabel="name"
25
15
bindValue="id"
26
- placeholder="Select city"
27
- formControlName="city">
16
+ [multiple]="true"
17
+ placeholder="Select cities"
18
+ formControlName="selectedCitiesIds">
28
19
</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>
29
23
</div>
30
-
24
+ <hr>
31
25
<div class="form-group">
32
26
<label for="state">Age</label>
33
27
<ng-select [items]="ages"
@@ -38,7 +32,7 @@ import {HttpClient} from '@angular/common/http';
38
32
<br>
39
33
<button class="btn btn-secondary btn-sm" (click)="toggleAgeDisable()">Toggle disabled</button>
40
34
</div>
41
-
35
+ <hr>
42
36
<div class="form-group">
43
37
<label for="album">Favorite album</label>
44
38
<ng-select [items]="albums"
@@ -53,7 +47,7 @@ import {HttpClient} from '@angular/common/http';
53
47
</ng-select>
54
48
<small class="form-text text-muted">Albums data from backend using HttpClient.</small>
55
49
</div>
56
-
50
+ <hr>
57
51
<div class="form-group">
58
52
<label for="album">Favorite photo</label>
59
53
<ng-select [items]="photos"
@@ -81,10 +75,12 @@ export class ReactiveFormsComponent {
81
75
82
76
heroForm : FormGroup ;
83
77
78
+ isCitiesControlVisible = true ;
84
79
cities : NgOption [ ] = [
85
80
{ id : 1 , name : 'Vilnius' } ,
86
81
{ id : 2 , name : 'Kaunas' } ,
87
- { id : 3 , name : 'Pavilnys' , disabled : true }
82
+ { id : 3 , name : 'Pavilnys (Disabled)' , disabled : true } ,
83
+ { id : 4 , name : 'Pabradė' } ,
88
84
] ;
89
85
90
86
ages : NgOption [ ] = [
@@ -105,9 +101,7 @@ export class ReactiveFormsComponent {
105
101
this . loadPhotos ( ) ;
106
102
107
103
this . heroForm = this . fb . group ( {
108
- name : [ '' , Validators . required ] ,
109
- street : '' ,
110
- city : '' ,
104
+ selectedCitiesIds : [ ] ,
111
105
age : '' ,
112
106
album : '' ,
113
107
photo : ''
@@ -122,6 +116,14 @@ export class ReactiveFormsComponent {
122
116
}
123
117
}
124
118
119
+ toggleCitiesControl ( ) {
120
+ this . isCitiesControlVisible = ! this . isCitiesControlVisible ;
121
+ }
122
+
123
+ clearCities ( ) {
124
+ this . heroForm . get ( 'selectedCitiesIds' ) . patchValue ( [ ] ) ;
125
+ }
126
+
125
127
private loadAlbums ( ) {
126
128
this . http . get < any [ ] > ( 'https://jsonplaceholder.typicode.com/albums' ) . subscribe ( rsp => {
127
129
this . albums = rsp ;
0 commit comments