Skip to content

Commit d3c1b23

Browse files
committed
feat: update with reactive form information
1 parent 89e2a61 commit d3c1b23

File tree

1 file changed

+106
-13
lines changed

1 file changed

+106
-13
lines changed

README.md

Lines changed: 106 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,120 @@
1+
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
2+
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
3+
4+
- [Project](#project)
5+
- [License](#license)
6+
- [Angular の form について](#angular-%E3%81%AE-form-%E3%81%AB%E3%81%A4%E3%81%84%E3%81%A6)
7+
- [リアクティブフォーム](#%E3%83%AA%E3%82%A2%E3%82%AF%E3%83%86%E3%82%A3%E3%83%96%E3%83%95%E3%82%A9%E3%83%BC%E3%83%A0)
8+
- [使用方法](#%E4%BD%BF%E7%94%A8%E6%96%B9%E6%B3%95)
9+
- [form のデータ更新](#form-%E3%81%AE%E3%83%87%E3%83%BC%E3%82%BF%E6%9B%B4%E6%96%B0)
10+
- [form の validate](#form-%E3%81%AE-validate)
11+
- [テンプレート駆動フォーム](#%E3%83%86%E3%83%B3%E3%83%97%E3%83%AC%E3%83%BC%E3%83%88%E9%A7%86%E5%8B%95%E3%83%95%E3%82%A9%E3%83%BC%E3%83%A0)
12+
- [使用方法](#%E4%BD%BF%E7%94%A8%E6%96%B9%E6%B3%95-1)
13+
- [起動](#%E8%B5%B7%E5%8B%95)
14+
15+
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
16+
117
# Project
218

3-
This project was generated with [Angular CLI](https://github.com/angular/angular-cli) version 9.1.1.
19+
This project was generated with [Angular CLI](https://github.com/angular/angular-cli) version 10.1.2.
20+
21+
## License
22+
23+
MIT
24+
25+
## Angular の form について
26+
27+
angular には 2 つのフォームスタイルがあり、それぞれ使用方法も思想も違います。
28+
このレポジトリはそれぞれの使い方をまとめています。
29+
30+
### リアクティブフォーム
31+
32+
#### 使用方法
33+
34+
'@angular/forms' の提供する FormBuilder, FormControl, FormGroup を使用します。
35+
36+
- ① コンポーネントと HTML 要素の form を formGroup で指定し、リンクさせる
37+
- ② formControlName を指定し、コンポーネントのフォーム属性とリンクさせる
38+
39+
```html
40+
<!-- ① using formGroup to link form with component -->
41+
<form class="form" [formGroup]="loginForm" (ngSubmit)="submit()">
42+
<!-- ② using formControlName to link form with component's attribute -->
43+
<input type="email" placeholder="mail" formControlName="mail" required />
44+
<input
45+
type="password"
46+
placeholder="Password"
47+
formControlName="password"
48+
required
49+
/>
50+
</form>
51+
```
52+
53+
- ③-1 フォームで使用する対象の属性を FormBuilder 経由で初期化する
54+
55+
```typescript
56+
// Using FormBuilder to link and initialize form values
57+
public loginForm = this.builder.group({
58+
mail: [''],
59+
password: [''],
60+
});
61+
```
62+
63+
- ③-2 フォームで使用する対象の属性を FormGroup, FormControl 経由で初期化する
64+
65+
```typescript
66+
// This also possible though it could be troublesome (using instance..)
67+
public loginForm = new FormGroup({
68+
mail: new FormControl(''),
69+
password: new FormControl(''),
70+
});
71+
```
472

5-
## Development server
73+
#### form のデータ更新
674

7-
Run `ng serve` for a dev server. Navigate to `http://localhost:4200/`. The app will automatically reload if you change any of the source files.
75+
- FormGroup のメソッド経由で値を更新する
876

9-
## Code scaffolding
77+
```typescript
78+
// 特定の要素のみ値を更新する際には patchValue を使用する
79+
this.loginForm.patchValue({
80+
mail: 'sample@example.com',
81+
});
1082

11-
Run `ng generate component component-name` to generate a new component. You can also use `ng generate directive|pipe|service|class|guard|interface|enum|module`.
83+
// すべての要素を更新する際には setValue を使用する
84+
this.loginForm.setValue({
85+
mail: 'sample@example.com',
86+
password: 'password',
87+
});
88+
```
1289

13-
## Build
90+
#### form の validate
1491

15-
Run `ng build` to build the project. The build artifacts will be stored in the `dist/` directory. Use the `--prod` flag for a production build.
92+
- formGroup で Validators 経由の validate をかける
93+
- input 要素に required を追加する
1694

17-
## Running unit tests
95+
```typescript
96+
// Validators 経由で Validate をかける
97+
public loginForm = this.builder.group({
98+
mail: ['', Validators.email], // validates whether email format or not
99+
password: ['', Validators.required], // validates form filled or not
100+
});
101+
```
18102

19-
Run `ng test` to execute the unit tests via [Karma](https://karma-runner.github.io).
103+
```html
104+
<!-- html の input要素に require を追加 -->
105+
<input type="email" placeholder="mail" formControlName="mail" required />
106+
<input
107+
type="password"
108+
placeholder="Password"
109+
formControlName="password"
110+
required
111+
/>
112+
```
20113

21-
## Running end-to-end tests
114+
### テンプレート駆動フォーム
22115

23-
Run `ng e2e` to execute the end-to-end tests via [Protractor](http://www.protractortest.org/).
116+
#### WIP
24117

25-
## Further help
118+
## 起動
26119

27-
To get more help on the Angular CLI use `ng help` or go check out the [Angular CLI README](https://github.com/angular/angular-cli/blob/master/README.md).
120+
`ng serve` で 起動します.

0 commit comments

Comments
 (0)