์๋ ํ์ธ์ Foma ๐ป ์ ๋๋ค.
์ค๋์ ํ์๊ฐ์ ์ ํ ๋ ์ ํด์ง ๊ท์น์ ๋ง๋์ง ์ฌ๋ถ๋ฅผ ๊ฒ์ฌํ๋ ํ๋ฉด ํผ์ ๋ง๋ค์ด ๋ณด๊ฒ ์ต๋๋ค.
๋ฐ๋ก ์์ํ ๊ฒ์~
Set up angular material
์ต๊ทค๋ฌ์์๋ ๋จธํฐ๋ฆฌ์ผ ๋์์ธ๋ UI ์ปดํฌ๋ํธ๋ค์ ์ ๊ณตํฉ๋๋ค.
์๋ ์ฌ์ดํธ๋ฅผ ์ด๋ํ์๋ฉด ์ฌ๋ฌ ์ปดํฌ๋ํธ๋ค์ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ๊ณผ API๋ฅผ ํ์ธํ ์ ์์ต๋๋ค.
Angular Material
UI component infrastructure and Material Design components for Angular web applications.
material.angular.io
ํฐ๋ฏธ๋์ angular material์ ์ค์นํด ์ค๋๋ค.
ng add @angular/material
์ค์น๊ฐ ์๋ฃ๋ ๋ค์ ๋ช๊ฐ์ง ์ถ๊ฐ์ ์ผ๋ก ์ค์นํ ๊ฑด์ง ๋ฌผ์ด๋ณด๋๋ฐ์.
Would like to proceed? (๊ณ์ ์งํํ ๊ฑฐ์ผ?) yes
Choose a prebuilt theme name, or "custom" for a custom theme (๋๊ฐ ์ํ๋ ํ ๋ง ์๊น์ด๋, ์ปค์คํ ํ ๋ง ๊ณจ๋ผ๋ด)
๋ง์ ๋๋ ํ ๋ง ๊ณ ๋ฅด์๋ฉด ๋ฉ๋๋ค.
Set up global Angular Material typography styles? (๋ ์ต๊ทค๋ฌ ํ์ดํฌ๊ทธ๋ํผ ์ฌ์ฉํ ๋?) yes
Set up browser animations for Angular Material? (์ต๊ทค๋ฌ ๋จธํฐ๋ฆฌ์ผ ๋ธ๋ผ์ฐ์ ์ ๋๋ฉ์ด์ ์ค์นํ ๋?) yes
Set up material components
ํ์๊ฐ์ ํ๋ฉด์์ ํ์ํ ์ปดํฌ๋ํธ๋ค์ ๋จผ์ ์์๋ณด๊ฒ ์ต๋๋ค.
1. Forms Field
์ ๋ ฅ ์์์ ํ๋์ ๊ฐ์ ์ธํ ํ๊ธฐ ์ํด ์ฌ์ฉ๋ฉ๋๋ค/
2. Input
์ ๋ ฅํ๋ ์ฐฝ์ ์ํด ์ฌ์ฉ๋ฉ๋๋ค.
3. Button
ํ์๊ฐ์ ๋ฒํผ์ ๋ง๋ค ๋ ์ฌ์ฉ๋ฉ๋๋ค.
app.module.ts
์์์ ์ธ๊ธํ ์ปดํฌ๋ํธ๋ค์ import ํด์ค๋๋ค.
๊ทธ๋ฆฌ๊ณ Material Component๋ ์๋์ง๋ง ์ ์ปดํฌ๋ํธ๋ค์ ์ฌ์ฉํ๊ธฐ ์ํด ํ์ํ ReactiveFormsModule, FormsModule๋ฅผ import ํด์ค๋๋ค.
import { MatFormFieldModule } from '@angular/material/form-field';
import { MatInputModule } from '@angular/material/input';
import { MatButtonModule } from '@angular/material/button';
import { ReactiveFormsModule, FormsModule } from '@angular/forms';
NgModule์๋ ์ถ๊ฐ์์ผ ์ค๋๋ค.
@NgModule({
...
imports: [
BrowserModule,
AppRoutingModule,
BrowserAnimationsModule,
MatFormFieldModule,
MatInputModule,
MatButtonModule,
ReactiveFormsModule,
FormsModule
],
...
})
Create register component
ํ์๊ฐ์ ์ปดํฌ๋ํธ๋ฅผ ์์ฑํด ์ค๋๋ค.
ng g c pages/register
register.component.html
์์์ ์ค์นํด์ค ์ปดํฌ๋ํธ๋ค์ ํ์ฉํ์ฌ ํ์๊ฐ์ ํ๋ฉด ๋ ์ด์์์ ์ก์์ค๋๋ค.
<div class="container">
<div class="form-container">
<div class="title">Register</div>
<form>
<mat-form-field appearance="outline">
<mat-label>Enter your email</mat-label>
<input matInput formControlName="email" />
</mat-form-field>
<mat-form-field appearance="outline">
<mat-label>Enter your password</mat-label>
<input type="password" matInput formControlName="password" />
</mat-form-field>
</form>
<div class="buttons-container">
<button
mat-raised-button
class="register-button">
Register
</button>
</div>
</div>
</div>
register.component.css
.container {
display: flex;
flex-direction: row;
width: 100%;
height: 100%;
justify-content: center;
align-items: center;
}
.form-container {
display: flex;
flex-direction: column;
align-items: center;
margin-top: 10vh;
margin-bottom: 10vh;
background-color: white;
box-shadow: 0 2px 6px 1px rgba(0, 0, 0, 0.2);
padding: 50px;
}
form {
display: flex;
flex-direction: column;
}
.title {
font-size: 22px;
margin-bottom: 20px;
}
mat-form-field {
width: 20vw;
}
.buttons-container {
display: flex;
flex-direction: column;
width: 100%;
align-items: center;
}
.buttons-container > p {
margin-top: 10px;
}
Register Layout
์คํ์์ผ๋ณด๋ฉด ์๋์ ๊ฐ์ ํ๋ฉด์ด ๋์ต๋๋ค.
Create form validator
์ด์ Input์ฐฝ์ ์ด๋ ํ ๊ฐ์ ์ ๋ ฅํ์ ๋ ์ ํด์ง ๊ท์น๋๋ก ์์ฑํ๋์ง ๊ฒ์ฌํ ์ฝ๋๋ฅผ ์์ฑํด ๋ณด๊ฒ ์ต๋๋ค.
FormBuilder๋ฅผ import ํด์ฃผ๊ณ ,
import { FormBuilder } from '@angular/forms';
์์ฑ์์ formBuilder๋ฅผ ๋ง๋ค์ด ์ค๋๋ค.
constructor(private formBuilder:FormBuilder,) { }
formBuilder๋ form์ ๊ด๋ จ๋ formControl,validator ๋ฑ์ ๋ง๋ค ์ ์๋ ํด๋์ค ์ ๋๋ค.
registerform์ ๊ท์น๋ค์ ์์ฑํด ์ฃผ๊ฒ ์ต๋๋ค.
์๋์ ๊ฐ์ด form์ ๋ง๋ค์ด์ฃผ๊ณ , group์ ํตํด์ ๊ฐ input ๊ฐ์ ๊ท์น๋ค์ ์ ์ด์ค๋๋ค.
registerForm = this.formBuilder.group({
//๋ฐ๋์ ์
๋ ฅ๋์ด์ผ ํ๋ฉฐ, ์ด๋ฉ์ผ ํ์์ ๋ง์ถฐ ์์ฑํด์ผ ํจ.
email: ['', [Validators.required, Validators.email]],
//๋ฐ๋์ ์
๋ ฅ๋์ด์ผ ํ๋ฉฐ, ์ต์ 6๊ธ์ ์ด์์ด์ด์ผ ํจ.
password: ['', [Validators.required, Validators.minLength(6)]],
});
register.component.html
htmlํ์ผ์์ form์ ์์์ ๋ง๋ registerForm์ ์ ์ฉ์์ผ ์ค๋๋ค.
<form [formGroup]="registerForm">
...
</form>
๊ทธ๋ฆฌ๊ณ ์คํ์์ผ ๋ณด๋ฉด registerForm์ ์ ์ฉํ ๊ท์น๋๋ก ์์ฑํ์ง ์์ผ๋ฉด ๋นจ๊ฐ์์ผ๋ก, ๊ท์น์ ์ค์ํ๋ฉด ํ๋์์ผ๋ก Input ์ฐฝ์ด ๋ฐ๋๋ ๊ฑธ ๋ณผ ์ ์์ต๋๋ค.
Button activate/deactivate
์ ๋ ฅํ ๊ฐ๋ค์ด ์ ํด์ง ๊ท์น์ ์ค์ํ์ง ์๋๋ค๋ฉด ๋ฒํผ์ด ๋นํ์ฑํ ๋๊ณ , ์ค์ํ๋ค๋ฉด ํ์ฑํ ๋๊ฒ ํ๋ฉด ๋ ๋ซ๊ฒ ์ฃ ?
๋ฐ๋ก ์์ ๋ง๋ค์๋ registerForm์ ์ด์ฉํด์ ๋ฒํผ๊ณผ ๋ฐ์ธ๋ฉ ์์ผ์ค๋๋ค.
register.component.html
๋ฒํผ์ ๋นํ์ฑํ ์ํค๋๋ก ํ๋ disabled์ registerForm์ด ์ฌ๋ฐ๋ฅธ์ง ์๋์ง๋ฅผ ์๋ ค์ค .invalid ๋ฉ์๋๋ฅผ ์ฌ์ฉํด์ ๋ฐ์ธ๋ฉ ํด์ค๋๋ค.
...
<button
[disabled]="registerForm.invalid"
mat-raised-button
class="register-button">
Register
</button>
...
์ด๋ ๊ฒ ๋ฐ์ธ๋ฉ ์ํค๋ฉด ํผ์ด ๊ท์น์ ์ค์ํ๋์ง ์ํ๋์ง์ ๋ฐ๋ผ ์๋์ ๊ฐ์ด ๋ฒํผ์ด ํ์ฑํ/๋นํ์ฑํ ๋๊ฒ ๋ฉ๋๋ค.
Show error messages below
์ฌ์ฉ์์๊ฒ ์ด๋ค ๊ท์น์ ์ง์ผ์ผ ํ๋์ง ์๋ ค์ค์ผ ๊ณ ์น ์ ์๊ฒ ์ฃ ?
๋ฐ๋ก ์ ๋ ฅ์ฐฝ ์๋์ ๊ฒฝ๊ณ ๊ธ์จ๋ก ์ด๋ค ๊ท์น์ ์ค์ํด์ผ ํ๋์ง ์๋ ค์ฃผ๋ ๊ฒ์ ๊ตฌํํ๋๋ก ํ๊ฒ ์ต๋๋ค.
register.component.html
๋จผ์ ์ด๋ฉ์ผ ๊ฒฝ๊ณ ๋ฉ์์ง๋ถํฐ ๋ค๋ฃจ๊ฒ ์ต๋๋ค.
mat-error๋ ๋จธํฐ๋ฆฌ์ผ ๋ชจ๋์์ ์ ๊ณตํ๋ ์๋ฌ ํ์ ์ปดํฌ๋ํธ ์ ๋๋ค.
๊ทธ ๋ค์์ผ๋ก *ngIf๋ html์์ if๋ฌธ์ ์ฌ์ฉํ ์ ์๋ ๊ฑด๋ฐ์.
์ฌ๊ธฐ์ registerForm์ controls(ํผ์์ ๊ตฌํํ ํ๋กํผํฐ๋ค) ์ค email์ธ ์ด๋ฆ์ ๊ฐ์ง ๊ฒ์ ์๋ฌ๊ฐ ์๋์ง ์ ๋ฌด๋ฅผ ํ์ ํ๋ ๊ฒ์ ๋๋ค.
required ์๋ฌ๊ฐ ๋ฐ์ํ๋ค๋ฉด ์ ๋ ฅํ์ง ์์ ๊ฒ์ด๊ณ , required๊ฐ ์ ์์ด๋ผ๋ฉด ์ด๋ฉ์ผ ํ์์ ๋ง์ถ์ง ์์ ๊ฒ์ด๋ฏ๋ก ์๋์ ๊ฐ์ด ์๋ฌ ๋ฉ์ธ์ง๋ฅผ ๋์์ค๋๋ค.
<input matInput formControlName="email" />
<mat-error *ngIf="registerForm.controls['email'].invalid">
{{
registerForm.controls["email"].hasError("required")
? "์ด๋ฉ์ผ์ ๋น์นธ์ผ๋ก ๋๋ฉด ์๋ฉ๋๋คใ
"
: "์ด๋ฉ์ผ ํ์์ ๋ง๊ฒ ์ ์ด์ฃผ์ธ์ใ
"
}}
</mat-error>
ํจ์ค์๋๋ ๋์ผํ๊ฒ ์์ฑํด ์ค๋๋ค.
input ์ ๋ ฅ์ฐฝ ๋ฐ๋ก ์๋์ ์ฝ๋๋ฅผ ๋ฃ์ด์ฃผ์ธ์.
<input type="password" matInput formControlName="password" />
<mat-error *ngIf="registerForm.controls['password'].invalid">
{{
registerForm.controls["password"].hasError("required")
? "๋น๋ฐ๋ฒํธ๋ ๋น์นธ์ผ๋ก ๋๋ฉด ์๋ฉ๋๋ค ใ
"
: "์ต์ 6๊ธ์ ์ด์ ์
๋ ฅํ์
์ผ ํฉ๋๋ค ใ
"
}}
</mat-error>
์๋์ ๊ฐ์ด ์๋ฌ ๋ฉ์ธ์ง๊ฐ ๋จ๊ฒ ๋ฉ๋๋ค.
์ค๋์ ์ด๋ ๊ฒ ํ์๊ฐ์ ์ ๋ ฅ ํ์์ ๋ง๊ฒ ์ ํจ์ฑ ๊ฒ์ฌ,์ ๋ ฅ์ฐฝ,๋ฒํผ,๊ฒฝ๊ณ ๊ธ์จ ๋ฑ์ ๋ค๋ค๋ณด์๋๋ฐ์.
ํน์๋ผ๋ ํ๋ฆฐ ์ ์ด ์๊ฑฐ๋ ๊ถ๊ธํ์ ์ ์ด ์๋ค๋ฉด ์ธ์ ๋ ๋๊ธ๋ก ์๋ ค์ฃผ์ธ์!
'๐ป Frontend > Angular' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Angular] Pipe๋? (feat. Built-in Pipe) (0) | 2022.05.24 |
---|---|
[Angular] ๋ชจ๋ฐ์ผ ๋ธ๋ผ์ฐ์ ํ๋ฉด ๊ณ ์ ํ๊ธฐ (Lock Screen orientation in mobile browser) (2) | 2022.05.05 |
[Angular] Router์ ๋ํด ์์๋ณด๊ธฐ (feat. ํ๋ฉด์ด๋) (0) | 2022.03.24 |
[Angular] NgForm์ด๋? (feat. ngModel,ngSubmit,required) (0) | 2022.03.15 |
[Angular] ๋ฒํผ ์ด๋ฒคํธ ์ ์ฉํด๋ณด๊ธฐ (Button Event) (0) | 2022.03.15 |
๋๊ธ