Architecture - nkiateam/angular-tutorial GitHub Wiki
Angular ์์ฉ ํ๋ก๊ทธ๋จ์ ๊ธฐ๋ณธ ๊ตฌ์ฑ ์์
Angular๋ HTML๋ก ์์ฑ๋ ํด๋ผ์ด์ธํธ ์์ฉ ํ๋ก๊ทธ๋จ๊ณผ JavaScript ๋๋ Dart ๋๋ TypeScript์ ๊ฐ์ JavaScript๋ก ์ปดํ์ผํ๋ ์ธ์ด ์ค ํ๋์ ๋๋ค.
ํ๋ ์์ํฌ๋ ๋ช ๊ฐ์ ์ฝ์ด ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ๋๋จธ์ง ์ ํ์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ก ๊ตฌ์ฑ๋์ด ์์ต๋๋ค.
Angularized ๋งํฌ ์ ์ ์ฌ์ฉํ์ฌ HTML ํ ํ๋ฆฟ์ ์์ฑํ๊ณ ์ด๋ฌํ ํ ํ๋ฆฟ์ ๊ด๋ฆฌํ๊ธฐ ์ํ ๊ตฌ์ฑ ์์๋ก ํด๋์ค ์์ฑ, ์๋น์ค์ ์์ฉ ํ๋ก๊ทธ๋จ ๋ ผ๋ฆฌ ์ถ๊ฐ ๋ฐ ๋ชจ๋์ ๋ณต์ฑ ๊ตฌ์ฑ ์์ ๋ฐ ์๋น์ค ์ถ๊ฐ๋ฅผ ํตํด ๊ฐ๋ ์์ฉ ํ๋ก๊ทธ๋จ์ ์์ฑํ ์ ์์ต๋๋ค.
๊ทธ๋ฐ ๋ค์ ๋ฃจํธ ๋ชจ๋์ ๋ถํธ ์คํธ๋ฉํ์ฌ ์ฑ์ ์คํํฉ๋๋ค. Angular๋ ๋ธ๋ผ์ฐ์ ์์ ์ ํ๋ฆฌ์ผ์ด์ ์ฝํ ์ธ ๋ฅผ ์ ๊ณตํ๊ณ ์ ๊ณต ํ ์ง์นจ์ ๋ฐ๋ผ ์ฌ์ฉ์ ์ํธ ์์ฉ์ ์๋ตํฉ๋๋ค.
๋ฌผ๋ก , ์ด๊ฒ๋ณด๋ค ๋ ๋ง์ ๊ฒ๋ค์ด ์์ต๋๋ค. ๋ค์ ํ์ด์ง์์ ์ธ๋ถ ์ ๋ณด๋ฅผ ๋ฐฐ์ฐ๊ฒ ๋ฉ๋๋ค. ์ง๊ธ์ ํฐ ๊ทธ๋ฆผ์ ์ง์คํ์ญ์์ค.

The architecture diagram identifies the eight main building blocks of an Angular application:
Learn these building blocks, and you're on your way.
The code referenced on this page is available as a live example.
Modules
Angular apps are modular and Angular has its own modularity system called Angular modules or NgModules.
Angular modules are a big deal. This page introduces modules; the Angular modules page covers them in depth.
Every Angular app has at least one module, the root module, conventionally named AppModule.
While the root module may be the only module in a small application, most apps have many more feature modules, each a cohesive block of code dedicated to an application domain, a workflow, or a closely related set of capabilities.
An Angular module, whether a root or feature, is a class with an @NgModule decorator.
Decorators are functions that modify JavaScript classes. Angular has many decorators that attach metadata to classes so that it knows what those classes mean and how they should work. Learn more about decorators on the web.
NgModule is a decorator function that takes a single metadata object whose properties describe the module. The most important properties are:
declarations - the view classes that belong to this module. Angular has three kinds of view classes: components, directives, and pipes.
exports - the subset of declarations that should be visible and usable in the component templates of other modules.
imports - other modules whose exported classes are needed by component templates declared in this module.
providers - creators of services that this module contributes to the global collection of services; they become accessible in all parts of the app.
bootstrap - the main application view, called the root component, that hosts all other app views. Only the root module should set this bootstrap property.
Here's a simple root module:
app/app.module.ts
COPY CODE
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
@NgModule({
imports: [ BrowserModule ],
providers: [ Logger ],
declarations: [ AppComponent ],
exports: [ AppComponent ],
bootstrap: [ AppComponent ]
})
export class AppModule { }
The export of AppComponent is just to show how to export; it isn't actually necessary in this example. A root module has no reason to export anything because other components don't need to import the root module.
Launch an application by bootstrapping its root module. During development you're likely to bootstrap the AppModule in a main.ts file like this one.
app/main.ts
COPY CODE
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app.module';
platformBrowserDynamic().bootstrapModule(AppModule);
####Angular modules vs. JavaScript modules The Angular module โ a class decorated with @NgModule โ is a fundamental feature of Angular.
JavaScript also has its own module system for managing collections of JavaScript objects. It's completely different and unrelated to the Angular module system.
In JavaScript each file is a module and all objects defined in the file belong to that module. The module declares some objects to be public by marking them with the export key word. Other JavaScript modules use import statements to access public objects from other modules.
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
export class AppModule { }
Learn more about the JavaScript module system on the web.
These are two different and complementary module systems. Use them both to write your apps.
####Angular libraries
Angular ships as a collection of JavaScript modules. You can think of them as library modules.
Each Angular library name begins with the @angular prefix.
You install them with the npm package manager and import parts of them with JavaScript import statements.
For example, import Angular's Component decorator from the @angular/core library like this:
import { Component } from '@angular/core';
You also import Angular modules from Angular libraries using JavaScript import statements:
import { BrowserModule } from '@angular/platform-browser';
In the example of the simple root module above, the application module needs material from within that BrowserModule. To access that material, add it to the @NgModule metadata imports like this.
imports: [ BrowserModule ],
In this way you're using both the Angular and JavaScript module systems together.
It's easy to confuse the two systems because they share the common vocabulary of "imports" and "exports". Hang in there. The confusion yields to clarity with time and experience.
Learn more from the Angular modules page.
Components

Templates
Metadata
Data binding
ํ๋ ์์ํฌ ์์ด ๋ฐ์ดํฐ ๋ฐ์ธ๋ฉ์ ํ๋ ๊ฒฝ์ฐ ๋ฐ์ดํฐ ๊ฐ์ HTML ์ปจํธ๋กค๋ก ํธ์ํ๊ณ ์ฌ์ฉ์์ ์๋ต์ ์ก์ ๋ฐ ๊ฐ ์ ๋ฐ์ดํธ๋ก ๋ณํํด์ผ ํฉ๋๋ค. ๊ทธ๋ฌํ push/pull ๋ก์ง์ ์ง์ ์์ฑํ๋ ๊ฒ์ ๋ก์ ๋ฐฉ์์ด๋ฉฐ ์ค๋ฅ ๋ฐ์์ ์ฌ์ง๊ฐ ์์ต๋๋ค.
์ด์ Angular2๋ ํ
ํ๋ฆฟ HTML์ผ๋ถ์ ๋ฐ์ธ๋ฉ ๋งํฌ์
์ ์ถ๊ฐํ์ฌ ์์ชฝ์ ์ฐ๊ฒฐํ๊ณ
๊ตฌ์ฑ์์์ ์ผ๋ถ์ ์กฐ์จํ๋ ๋ฉ์ปค๋์ฆ์ธ ๋ฐ์ดํฐ ๋ฐ์ธ๋ฉ์ ์ง์ํฉ๋๋ค.
๋ค์ด์ด ๊ทธ๋จ์์ ๋ณผ ์ ์๋ฏ์ด 4๊ฐ์ง ํํ์ ๋ฐ์ธ๋ฉ ๊ตฌ๋ฌธ์ด ์์ต๋๋ค.
๊ฐ ์์์ DOM๊ฐ์ ์๋ฐฉํฅ์ ๋ํ๋
๋๋ค.
์๋ ์์ ํ ํ๋ฆฟ์๋ 3๊ฐ์ง ํ์์ด ์์ต๋๋ค.
app/hero-list.component.html (binding) <li>{{hero.name}}</li> <hero-detail [hero]="selectedHero""></hero-detail> <li (click)="selectHero(hero)"></li>
{{hero.name}} - <li> ํ๊ทธ ๋ด์ hero.name์ ์์ฑ ๊ฐ์ ํ์ถํฉ๋๋ค. [hero] - selectedHero ๊ฐ์ ๋ถ๋ชจ์ธ HeroListComponent์์ ์์์ธ HeroDetailComponent์ hero ์์ฑ์ผ๋ก ์ ๋ฌํฉ๋๋ค. (click) - ์ฌ์ฉ์๊ฐ hero๋ฅผ ํด๋ฆญํ๋ฉด ์ด๋ฒคํธ ๋ฐ์ธ๋ฉ์ ๊ตฌ์ฑ์์์ seelectHero ๋ฉ์๋๋ฅผ ํธ์ถํฉ๋๋ค.
์๋ฐฉํฅ ๋ฐ์ดํฐ ๋ฐ์ธ๋ฉ์ ngModel ์ง์๋ฌธ์ ์ฌ์ฉํ์ฌ ์์ฑ ๋ฐ ์ด๋ฒคํธ ๋ฐ์ธ๋ฉ์ ๋จ์ผ ํ๊ธฐ๋ฒ์ผ๋ก ๊ฒฐํฉํ๋ ์ค์ํ 4๋ฒ์งธ ํ์์ ๋๋ค. ๋ค์์ HeroDetailComponet ํ ํ๋ฆฟ์ ์์ ์ ๋๋ค.
app/hero-detail.component.html (ngModel) <input [(ngModel)]="hero.name">
์๋ฐฉํฅ ๋ฐ์ธ๋ฉ์์ ๋ฐ์ดํฐ ์์ฑ ๊ฐ์ ์์ฑ ๋ฐ์ธ๋ฉ๊ณผ ๋ง์ฐฌ๊ฐ์ง๋ก ๊ตฌ์ฑ์์์ ์ ๋ ฅ์์๋ก ์ด๋ํฉ๋๋ค. ์ฌ์ฉ์์ ๋ณ๊ฒฝ์ฌํญ์ ๊ตฌ์ฑ์์๋ก ๋ค์ ์ ๋ฌ๋์ด ์ด๋ฒคํธ ๋ฐ์ธ๋ฉ๊ณผ ๋ง์ฐฌ๊ฐ์ง๋ก ๋ฑ๋ก ์ ๋ณด๋ฅผ ์ต์ ๊ฐ์ผ๋ก ์ฌ์ค์ ํฉ๋๋ค.
Angular๋ ๋ชจ๋ ์์ ๊ตฌ์ฑ์์๋ฅผ ํตํด ์์ฉํ๋ก๊ทธ๋จ ๊ตฌ์ฑ์์ ํธ๋ฆฌ์ ๋ฃจํธ์์ Event Cycle๋ง๋ค ํ ๋ฒ ์ฉ ๋ชจ๋ ๋ฐ์ดํฐ ๋ฐ์ธ๋ฉ์ ์ฒ๋ฆฌํฉ๋๋ค.

๋ฐ์ดํฐ ๋ฐ์ธ๋ฉ์ ํ ํ๋ฆฟ๊ณผ ํด๋น ๊ตฌ์ฑ์์๊ฐ์ ํต์ ์์ ์ค์ํ ์ญํ ์ ํฉ๋๋ค. ๋ถ๋ชจ / ์์๊ฐ์ ๋ฐ์ดํฐ ๋ฐ์ธ๋ฉ์ ๋ถ๋ชจ ๊ตฌ์ฑ์์์ ํ์ ๊ตฌ์ฑ ์์๊ฐ์ ํต์ ์๋ ์ค์ํฉ๋๋ค.
Directives

Angular๋ ํ ํ๋ฆฟ์ ๋ ๋๋ง ํ ๋ ์ง์์ด์ ๋ฐ๋ผ DOM์ ๋ณํํ๋ ๋์ ์ธ ๋ฐฉ์์ ๊ฐ๊ณ ์์ต๋๋ค.
์ง์๋ฌธ์ Metadata๋ฅผ ๊ฐ๊ณ ์๋ ํด๋์ค์ ๋๋ค. Typescript์์ @Directive ๋ฐ์ฝ๋ ์ดํฐ๋ฅผ ์ ์ฉํ์ฌ Metadata๋ฅผ ํด๋์ค์ ์ฒจ๋ถํฉ๋๋ค.
์ปดํฌ๋ํธ๋ ๋์ ์ธ ํ ํ๋ฆฟ ๊ตฌ์กฐ๋ฅผ ๊ฐ๊ณ ์์ต๋๋ค. @Component ๋ฐ์ฝ๋ ์ดํฐ๋ ์ค์ ๋ก ํ ํ๋ฆฟ ์งํฅ๊ธฐ๋ฅ์ผ๋ก ํ์ฅ๋ @Deirective ๋ฐ์ฝ๋ ์ดํฐ์ ๋๋ค.
Component๋ ๋์ ์ธ ๋ฐฉ์์ด์ง๋ง ์ํคํ ์ฒ์ ๊ฐ์๋ ๊ตฌ์ฑ์์์ ์ง์๋ฌธ์ ๊ตฌ๋ถํฉ๋๋ค.
์ง์์ด๋ 2๊ฐ์ง์ ์ข ๋ฅ๋ก ๊ตฌ์กฐ ์ง์์์ ์์ฑ ์ง์์๋ก ๋ถ๋ฅ๋ฉ๋๋ค. Attribute์ฒ๋ผ ์๋ฆฌ๋จผํธ ํ๊ทธ ์์ ์ฌ์ฉ๋๊ธฐ๋ ํ๊ณ ๋๋ก๋ ์ด๋ฆ์ผ๋ก ํ์๋๊ธฐ๋ ํ์ง๋ง ํ ๋น์ด๋ ๋ฐ์ธ๋ฉ์ ๋์์ผ๋ก ์์ฃผ ๋ฑ์ฅํ๋ ๊ฒฝํฅ์ด ์์ต๋๋ค.
- ๊ตฌ์กฐ ์ง์๋ฌธ
- DOM์ ์์๋ฅผ ์ถ๊ฐ, ์ ๊ฑฐ ๋ฐ ๊ต์ฒดํ์ฌ ๋ ์ด์์ ๊ตฌ์กฐ๋ฅผ ๋ณ๊ฒฝํ ์ ์์ต๋๋ค. ์์ ํ ํ๋ฆฟ์ 2๊ฐ์ง ๊ธฐ๋ณธ ๊ตฌ์กฐ ์ง์๋ฌธ์ ์ฌ์ฉํฉ๋๋ค.
app/hero-list.component.html (structural) <li *ngFor="let hero of heroes"></li> <hero-detail *ngIf="selectedHero"></hero-detail>
ngFor - heroes๋ชฉ๋ก์์ hero๋น ํ๋์ >li<๋ฅผ ์์ฑํ๋๋ก ๋ช ๋ นํฉ๋๋ค. ngIf - ์ ํ๋ hero๊ฐ ์๋ ๊ฒฝ์ฐ HeroDetail์ ๊ตฌ์ฑ์์๊ฐ ํฌํจ๋ฉ๋๋ค.
- ์์ฑ ์ง์๋ฌธ
- ๊ธฐ์กด ์์์ ๋ชจ์ ๋๋ ๋์์ ๋ณ๊ฒฝํฉ๋๋ค. ํ ํ๋ฆฟ ๋ด์์๋ ์ ๊ท HTML์์ฑ์ฒ๋ผ ๋ณด์ด๋ฏ๋ก ์์ฒด๊ฐ ์ด๋ฆ์ด ๋ฉ๋๋ค. ์๋ฐฉํฅ ๋ฐ์ดํฐ ๋ฐ์ธ๋ฉ์ ๊ตฌํํ๋ ngModel ์ง์ ๋ฌธ์ ์์ฑ ์ง์ ๋ฌธ์ ์์ ์ ๋๋ค. ngModel์ ํ์ ๊ฐ ์์ฑ์ ์ค์ ํ์ฌ ๋ณ๊ฒฝ ์ด๋ฒคํธ์ ์๋ตํ์ฌ ๊ธฐ์กด ์์(ex. <input">)์ ํ๋์ ๋ณ๊ฒฝํฉ๋๋ค.
app/hero-detail.component.html (ngModel) <input [(ngModel)]="hero.name">
Angular์๋ ๋ ์ด์์ ๊ตฌ์กฐ(ex: ngSwitch)๋ฅผ ๋ณ๊ฒฝํ๊ฑฐ๋ DOM ์์ ๋ฐ ๊ตฌ์ฑ์์์ ์ธก๋ฉด(ex: ngStyle, ngClass)์ ์์ ํ๋ ๋ช ๊ฐ์ง ์ง์๋ฌธ์ด ์ถ๊ฐ๋ก ์์ต๋๋ค.
๋ฌผ๋ก ์ฌ์ฉ์ ์ง์ ์ง์์ด๋ ์์ฑํ ์ ์์ต๋๋ค. HeroListComponent์ ๊ฐ์ ๊ตฌ์ฑ์์๋ ์ฌ์ฉ์ ์ง์ ์ง์๋ฌธ์ ํ ์ข ๋ฅ์ ๋๋ค.