Skip to content

Commit 165de76

Browse files
author
VladimirAmiorkov
committed
chore: update reactive forms example
1 parent 3141e90 commit 165de76

File tree

3 files changed

+25
-30
lines changed

3 files changed

+25
-30
lines changed

demo-angular/src/app/examples/reactive-forms/reactive-forms-example.component.css

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,10 @@
2222
margin-left: 100;
2323
}
2424

25-
.small-picture {
26-
height: 70;
27-
margin: 20;
28-
}
29-
3025
.yellow-grid {
3126
background-color: #F5C518;
3227
}
3328

34-
.result-label {
35-
background-color: gray;
36-
color: white;
37-
padding: 10;
38-
margin-right: 20;
39-
}
40-
4129
ListView.picker-field {
4230
background-color: green;
4331
margin-left: 20;
@@ -57,3 +45,7 @@ ActionBar.picker-field {
5745
font-size: 20;
5846
font-weight: bold;
5947
}
48+
49+
.submit-button {
50+
margin: 20;
51+
}

demo-angular/src/app/examples/reactive-forms/reactive-forms-example.component.html

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
</ActionBar>
44
<GridLayout rows="100, *" columns="*, *" [formGroup]="movieForm" class="yellow-grid">
55
<Label row="0" col="0" text="Pick a movie:" class="black-label label-center field-name-label"></Label>
6-
<PickerField row="0" col="1" formControlName="movie" [(ngModel)]="selectedMovie" hint="select a movie"
7-
padding="10" pickerTitle="Pick a movie" class="picker-field" modalClass="myModal" textField="name" [items]="pickerItems">
6+
<PickerField row="0" col="1" formControlName="movie" hint="select a movie" padding="10" pickerTitle="Pick a movie"
7+
class="picker-field" modalClass="myModal" textField="name" [items]="pickerItems">
88
<ng-template let-item="item">
99
<GridLayout rows="auto, *" columns="*, auto" class="yellow-grid">
1010
<Label [text]="item.name" colSpan="2" class="black-label item-template-top-label"></Label>
@@ -14,13 +14,7 @@
1414
</ng-template>
1515
</PickerField>
1616

17-
<GridLayout row="1" col="0" colSpan="2" rows="auto, auto, auto, auto" columns="*, *" backgroundColor="white">
18-
<Label row="0" col="0" colSpan="2" text="Result" class="black-label field-name-label" marginTop="20"></Label>
19-
<Label row="1" col="0" text="ngModel.name: " class="black-label field-name-label"></Label>
20-
<Label row="1" col="1" [text]="selectedMovie.name"></Label>
21-
<Label row="2" col="0" text="ngModel.year: " class="black-label field-name-label"></Label>
22-
<Label row="2" col="1" [text]="selectedMovie.year" ></Label>
23-
<Label row="3" col="0" text="ngModel.imageUrl: " class="black-label field-name-label"></Label>
24-
<Image row="3" col="1" [src]="selectedMovie.imageUrl" class="small-picture"></Image>
17+
<GridLayout row="1" col="0" colSpan="2" rows="auto, *" backgroundColor="white">
18+
<Button class="submit-button" text="Submit form" (tap)="onSubmit()"></Button>
2519
</GridLayout>
2620
</GridLayout>>

demo-angular/src/app/examples/reactive-forms/reactive-forms-example.component.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Component, OnInit } from "@angular/core";
2-
import { FormBuilder, FormGroup, Validators } from "@angular/forms";
2+
import { FormBuilder, FormGroup, FormControl, Validators } from "@angular/forms";
33
import { RouterExtensions } from "nativescript-angular/router";
44
import { ObservableArray } from "tns-core-modules/data/observable-array/observable-array";
55

@@ -24,20 +24,29 @@ export class ReactiveFormsExampleComponent implements OnInit {
2424
new Movie("One Flew Over the Cuckoo's Nest", 9, 1975, "https://m.media-amazon.com/images/M/MV5BZjA0OWVhOTAtYWQxNi00YzNhLWI4ZjYtNjFjZTEyYjJlNDVlL2ltYWdlL2ltYWdlXkEyXkFqcGdeQXVyMTQxNzMzNDI@._V1_UX182_CR0,0,182,268_AL_.jpg"),
2525
new Movie(" Lawrence of Arabia", 10, 1962, "https://m.media-amazon.com/images/M/MV5BYWY5ZjhjNGYtZmI2Ny00ODM0LWFkNzgtZmI1YzA2N2MxMzA0XkEyXkFqcGdeQXVyNjUwNzk3NDc@._V1_UY268_CR2,0,182,268_AL_.jpg"),
2626
]);
27-
this.selectedMovie = this.pickerItems.getItem(0);
27+
28+
this.movieForm = new FormGroup({
29+
movie: new FormControl(this.pickerItems.getItem(0).name, Validators.required),
30+
});
2831
}
2932

3033
ngOnInit(): void { }
3134

32-
public movieForm: FormGroup = this.fb.group({
33-
movie: [undefined, Validators.required],
34-
});
35-
36-
public selectedMovie: Movie;
35+
public movieForm: FormGroup;
3736

38-
public goBack() {
37+
Í; public goBack() {
3938
this.routerExtensions.backToPreviousPage();
4039
}
40+
41+
public onSubmit() {
42+
let formMovieValue = this.movieForm.get("movie").value;
43+
console.log("Forms 'movie' value: ", formMovieValue);
44+
alert({
45+
title: "Forms 'movie' value:",
46+
message: `id: ${formMovieValue.id}\n` + `name: ${formMovieValue.name}\n` + `year: ${formMovieValue.year}`,
47+
okButtonText: "OK"
48+
});
49+
}
4150
}
4251

4352
class Movie {

0 commit comments

Comments
 (0)