Angular - HiroSung/Study GitHub Wiki
Angular SPA (Single Page Application)
- front-end
- Angular, React, Vue.js 3ํ์ .
- front-end์ history
- ์นํ์ค. ๋ธ๋ผ์ฐ์ . Chrome. MVC pattern. ๋ฑ
- ์์ ํ ๊ฑฐ๋ญ๋๊ณ ์์. ๊ฐ๋ฐ์ ์์ญ์ผ๋ก ๋์ด์ด.
- ์นํ์ค : HTML5: W3C Recommendation (https://html.spec.whatwg.org/multipage/)
- Angular...
- angular.io. ๊ตฌ๊ธ์ด ๋ง๋ฌ
- 2010๋ ๋ ํ๋ฐ์ jquery์ ๋ชจ๋ธ๊ณผ๋ ๋ค๋ฅธ mvc์ ์์์ backbone.js ์ด๋ค. (backbonejs.org). jQuery์ ์์์ ๋ฐ๊พผ๊ฒ.
- ๊ทธ ๋ค์ embarjs
- ๊ทธ ๋ค์์ผ๋ก angularjs ๋์ด. Angular2์๋ ๋ค๋ฆ. ์ฑ๋ฅ์ ๋ฌธ์ ๋ฐ์.
- React : F/B์ ์ฌ์ฉ์๋ฅผ 50์ต๋ช ๊ธฐ๋ฐ์ผ๋ก ๋ง๋ค๊ฒ ๋ค๋ ๋ฒ์๋ก ์ค๊ณํจ. ๊ทธ๋์ ๋ง๋ ๊ฒ์ react์. O2O๋ react๋ก ๊ฐ๊ณ ์์. why? ๊ฐ๋ฐ์๊ฐ ๋ง์.
- Vue.js : ์ค๊ตญไบบ ๋ง๋ ์ธ์ด. ๊ฐ๋ฐ์ ๊ด์ ์์ ์ฌ์.
- Angular๋ TypeScript ์ด๋ค.
- ๋ฐฐ์ธ ๋ด์ฉ Web Application (Framework) HTML / CSS / Javascript EcmaScript6 / TypeScript / Node.js Angular CLI / NPM Angular 5.x (6.x)... ํ์ฌ๋ 8.x : Components, Router, Service (Dependency Injection / Reactive Programming), Forms (Angular Form / Reactive Form)
- https://angular.io/ : ๊ต์ฌ๋ 6๋ฒ์ ผ. 5์ดํ๋ถํฐ ๋ง์ด ๋ฐ๋
- https://poiemaweb.com/ ์ ์ ์ด์ ๋ชจ. ํฌ์ด์๋ง. ์ฌ์ดํธ์ ๋๋ฌด๋~~~ ์ ์ ๋ฆฌ๋์ด ์์.
- ์น์ ๋ฐ์ . 2010๋ ๋ ์นํ์ค์ด ๋ง๋ค์ด์ง ์์ .
- ์นํ์ค. W3C. HTML5/CSS3/EcmaScript5
- Web Components : https://d2.naver.com/helloworld/188655, https://www.webcomponents.org/. ๋ฏธ๋์ ์น ํ๊ฒฝ์ ๋ฐ๊ฟ๊ฒ์ด๋ผ ์์. devide and concom. ํ๋ฉด์ ๋ถ๋ฆฌํ์ฌ ์ ์ฉํ๋ค. (์. div ๋จ์๋ก ๊ฐ๋ฐ)
- Angular์๋ web components ๊ฐ๋ ์ด ์ ์ฉ๋์ด ์์.
- ์น ์ปดํฌ๋ํธ : ์น ํ์ด์ง๋ฅผ ํน์ ๋ทฐ ๋จ์๋ก ๋ถ๋ฆฌ ๊ฐ๋ฅํ๊ฒ ํด ์ค. Custom Elements, HTML Imports, Template, Shadow DOM
- ECMA ์คํฌ๋ฆฝํธ์ ๋ํ์ฌ. (European Computer Manufacturers Association) 97๋ ๋์ ์ค์ฌ๋ฅผ ์์ํจ. ES5 ์์ฑ. ES6 ๊ธฐ๋ฅ์ถ๊ฐ.
- ECMAscript 2015, 2016 ์๊ฐ : Modules, Classes, Arrow Functions(=>) ๋๋ค์ ๋ฌธ๋ฒ ์ง์, Let and Const ์๋ก์ด ๋ณ์ ์ ์ธ ํค์๋, Default/Rest/Spread ํจ์ํ๋ผ๋ฏธํฐ ์ธ ๊ฐ์ง ์๋ก์ด ๊ธฐ๋ฅ.
- Angular ES6๋ฅผ ๊ทธ๋๋ก ์ฌ์ฉํ์ง ์์. but React๋ ๊ทธ๋๋ก ์ฌ์ฉํด์ ๋ณต์ก.
- vscode, nodejs ์ค์น
- javascript๋ ๋ณ์์ type์ ์๋ฉด ๋จ. (์ซ์, ๋ฌธ์, ๋ฐฐ์ด, ํจ์, object)
- ๋ฐ์ดํฐ๋ object ๋จ์๋ก ์ ๋ฌ๋จ.
// ๋ณ์์ 3๊ฐ์ง Type
// 1. ์ซ์
var my_number = 3;
var my_number2 = 3.14;
console.log( typeof my_number);
console.log( typeof my_number2);
// 2. ์คํธ๋ง ํ์
var my_string = 'Hello World';
console.log( my_string);
console.log( my_string.replace('World', 'EXO'));
// 3. Object ํ์
// 3.1 Object ํ์
์ธ ๊ฐ์ง
// 3.1.1 ๋ฐฐ์ด array - ๋ฐ์ดํฐ๋ฅผ ๋ค๋ฃจ๊ธฐ ์ํจ.
var my_arry = [1, 2, 3, 4, 5];
// 3.1.2 ํจ์
var my_function = function ( a, b) { // ํจ์ ํํ์ - ์ ์ธ์ ์ฌ์ฉ ๋ถ๊ฐ. ๋ช
ํํ์ฌ ์ ํธํจ.
return a + b;
};
// function ์์ ๊ณ => ์ ๊ณ body ์์ฑ : ES6 ์์ ๋์จ๊ฒ. ๋๋ค์ ๋ฌธ๋ฒ
var my_function3 = (a, b) => {
return a + b;
};
function my_function2 (a, b) { // ํจ์ ์ ์ธ์ - ์ ์ธ์ ์ ์ฌ์ฉ๊ฐ๋ฅ. hoisting ๊ฐ๋ฅ.
return a + b;
}
console.log( my_function( 1, 2));
// 3.1.3 Object ๊ฐ์ฒด
var my_object = { // {} ์ค๊ดํธ ์ฌ์ฉ
title: '์ค๋์ ๊ต์ก์ค',
author: 'DO',
likes: 100,
tags: ['javascript', 'angular', 'education'],
replys: [
{ author: 'Lee', content: '๋๋ ์'},
{ author: 'Gowang', content: '๋๋ ์ ํ์ธ์'}
]
};
- let, const ์ถ๊ฐ๋จ.
- const ์ฒ์ ์ ์๋ ๊ฐ์ ๋ณ๊ฒฝํ ์ ์์
- ์ฌํ ๋น์ด ํ์ํ ๋ณ์๋ let์ผ๋ก ์ ์.
- Type์ ๊ฐ์ง๊ณ ์๋ ์คํฌ๋ฆฝํธ์.
- number, string, boolean, array, function, object
- ๋ณ์๋ช : ํ์ ๋ช = value
- const ํจ์๋ช = function(๋ณ์:ํ์ ): ๋ฆฌํดํ์ { ... };
// ๋ฐ์ดํฐํ์
// number, string, boolean, array, function, object
// - ๋ณ์๋ช
: ํ์
๋ช
const my_number_ts: number = 3;
const my_string_ts: string = 'hello world';
const my_array_ts: Array<number> = [1, 2, 3, 4];
const my_array2_ts: Array<number> = [1, 2, 3, 'kim']; // -> error
const my_object_ts: Object = {
title: 'hello',
author: 'kim'
}
const my_object2_ts: any = {
title: 'hello',
author: 'kim'
}
const my_func = function(a: number, b: number): number {
return a + b;
};
- ์ฝ๋ฉ์ ์ฐจ์ด
- ๋ณ์๋ช ์ ์ดํดํ ์ ์๋ ๋ช ์นญ์ผ๋ก ์ ์ํจ. my_func_list... etc
- stackoverfllow ๋ง์ด ์ฐธ์กฐํ์์ผ๋ฉด ํจ.
Angular CLI (https://cli.angular.io/)
- ๋ฐฐํฌํ๊ฒฝ๊ณผ ๊ฐ๋ฐํ๊ฒฝ์ด ์์ดํด์ง๊ณ ์์.
- ์ค์บํด๋ฉ ํด : webpack ๊ณผ ๊ฐ์ ํด๋ก ํตํฉ๋๊ณ ์์.
- ์ค์น
npm install -g @angular/cli
- cli project ์์ฑ (ํ์ฌ ๋๋ ํ ๋ฆฌ)
ng new hello-angular routing - NO CSS - SCSS // ๊ด๋ จ๋ ํ์ผ๋ค์ด ์์ฑ๋จ.
vscode์์ ์คํํด๋ - ๋ง๋ ํด๋ ์ ํ
- node_modules : library
- root : ์ค์ ํ์ผ
- e2e : test ๊ด๋ จ ํ์ผ
- src : ๊ฐ๋ฐ๊ณผ ๊ด๋ จ๋ ํ์ผ
- ๊ฐ๋ฐํ๊ฒฝ ๊ตฌ์ฑํ๊ธฐ ์ํ ์น์๋ฒ ๊ตฌ์ฑ.
ng serve --open
D:\hello-angular>ng serve
10% building 3/3 modules 0 activei ๏ฝขwds๏ฝฃ: Project is running at http://localhost:4200/webpack-dev-server/
i ๏ฝขwds๏ฝฃ: webpack output is served from /
i ๏ฝขwds๏ฝฃ: 404s will fallback to //index.html
chunk {main} main.js, main.js.map (main) 47.8 kB [initial] [rendered]
chunk {polyfills} polyfills.js, polyfills.js.map (polyfills) 264 kB [initial] [rendered]
chunk {runtime} runtime.js, runtime.js.map (runtime) 6.15 kB [entry] [rendered]
chunk {styles} styles.js, styles.js.map (styles) 10 kB [initial] [rendered]
chunk {vendor} vendor.js, vendor.js.map (vendor) 3.81 MB [initial] [rendered]
Date: 2020-01-20T05:56:17.394Z - Hash: 3a64ed6363a44f91c693 - Time: 5445ms
** Angular Live Development Server is listening on **localhost:4200**, open your browser on http://localhost:4200/ **
i ๏ฝขwdm๏ฝฃ: Compiled successfully.
- chrome์์ http://localhost:4200 ํธ์ถํ๋ฉด ํ์ด์ง ์กฐํ๋จ.
- ์์ค๋ฅผ ์์ ํ๋ฉด ๋ฐ๋ก๋ฐ๋ก ๋ฐ์๋จ.
- ๋ฐฐํฌ๋ ๋ณ๋๋ก ์์.
- ์ธ๋ถ์ ์ผ๋ก ๊ตฌ์ฑํ๋ ๊ฒ์ด ์ข์.
- sample์ app ํ๋์ component ๋ฅผ ๊ตฌ์ฑํ๊ฒ์. ์ปดํฌ๋ํธ ํ๋๊ฐ ํ๋์ ๋๋ ํ ๋ฆฌ์ด๋ค -- ๊ทธ๋ฌ๋ฉด ์ค๋ณต๋๋ ํด๋๊ฐ ๋ง์์ง๋๋ฐ์?
- ์ปดํฌ๋ํธ(ํ๋ฉด)๋ ๊ณ์ธต์ ์ด๋ค. - ๊ทธ๋์ ์๋จ/๋ฉ๋ด/์ปจํ ์ธ /๋ฐํ ์ผ๋ก ๊ตฌ์ฑํ๋๋ผ๋ root ์ ์ปดํฌ๋ํธ๊ฐ ํ์ํ๋ค. - root/ top/left/content/footer. ๊ทธ๋์ 5๊ฐ์ ์ปดํฌ๋ํธ๊ฐ ์ต์ ํ์. ๋ก๊ธด์ top์ ์ํ ์ปดํฌ๋ํธ. - ๊ตฌ์ฑํ๊ธฐ ๋๋ฆ.
- app/app.components. (์๋์ 3๊ฐ์ ํ์ผ์ด ํ๋์ set์ด ๋จ)
- .html
- .scss
- .ts
- app/app.module.ts : ํ๋์ application๊ณผ ๊ด๋ จ๋ ๋ชจ๋. ์ฌ๋ฌ๊ฐ์ ๋ชจ๋๋ก ๋ง๋ค๋ฉด raise loading ์ ๊ตฌ์ฑํ ์ ์์.
- ๋๋ถ๋ถ ์๋ฌธ์๋ก ๋ง๋ฌ.
- ์ปดํฌ๋ํธ๋ช .components. ๋ก ์์ฑ๋จ.*
- ์ปดํฌ๋ํธ ์ถ๊ฐ ์์ฑํ๊ธฐ
ng generate component ์ด๋ฆ
D:\hello-angular>ng g c header
CREATE src/app/header/header.component.html (21 bytes)
CREATE src/app/header/header.component.spec.ts (628 bytes)
CREATE src/app/header/header.component.ts (270 bytes)
CREATE src/app/header/header.component.scss (0 bytes)
UPDATE src/app/app.module.ts (396 bytes)
- header.component.ts ํ์ผ์ด ์ค์ํจ. ๋ฐ์ดํฐ๊ฐ ์์นํ๊ฒ ๋๋ ํ์ผ. ํด๋์ค๋ก ๋ง๋ค์ด์ง๊ณ ์ปดํฌ๋ํธ ์ด๋ฆ์ ์นด๋ฉํํ๋ก ๋ง๋ค์ด์ง.
import { Component, OnInit } from '@angular/core';
@Component({
selector: **'app-header'**, ==> **ํ๊ทธ์ ์ด๋ฆ์**
templateUrl: './header.component.html',
styleUrls: ['./header.component.scss']
})
export class **HeaderComponent **implements OnInit {
constructor() { }
ngOnInit() {
}
}
- app.component.html ์์
์ถ๊ฐ
- ์ด๋ฐ ์์ผ๋ก ... ์งํํฉ๋๋ค. ์ฝ๋ค!
- *.html ํ์ผ๋ง ๋ณด๋๋ผ๋ ์ด๋ ํ ๋ด์ฉ์ผ๋ก ๊ตฌ์ฑ๋์ด ์๋์ง ๊ฐ๋ ์ฑ์ด ์์.
- Front-end ๊ฐ๋ฐ์ ์ข ํฉ์ ์ธ ์์ ์.
- ๋ง๋ค์ด์ง ํ๋ฉด์ angular๋ก ๋ฐ๊พธ๋ ์์ ์ ํ๊ณ ์ ํจ.
- bootstrap : ๋์์ธ ์ ์ธ ๊ด์ ์ ์ปดํฌ๋ํธ
- 2008๋ chrome v8 ๋ฐํ. v8์ js ์์ง์ด 200๋ฐฐ ๋นจ๋ผ์ง์ ๋ฐ๋ผ ๋ผ์ด์ธ๋ฌ์ด Node.js ๋ฐํ.
- node.js ๋ javascript์ OS๋ก ํฌํ ํ ๊ฒ์.
- node.js๋ ๋น๋๊ธฐ์์ผ๋ก ๋์ํ์ฌ ๋ ผ๋ธ๋กํนI/O ์ฒ๋ฆฌ.
npm (https://npmjs.com)
- javascript library๋ฅผ ๋ชจ์๋ centeral repository.
- ์ฃผ๋ก ์ด์๊ฐ ๋๋ javascript lib ์ฐพ์ ๋ณผ ์ ์์. ์) lodash. rxjs. ...
- node.js ์ฉ vs ๋ธ๋ผ์ฐ์ ์ฉ js ๋๋ ๋ชจ๋ ์ง์ํ๋ universal ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ก ๊ตฌ๋ถ. rxjs๋ ๋ชจ๋ ์ฌ์ฉ.
- npm์ผ๋ก ์ค์น๋๋ฉด project root์ package.json ํ์ผ์ด ์์.
- package.json ์ javascript ํ๋ก์ ํธ์ ๋ฉ์ธ์ค์ ํ์ผ์.
- ์ค์ ํ์ผ...
- .json ์คํ์ผ๋ก.
- yaml : ์ต๊ทผ์๋ phthon์ ๋ฌธ๋ฒ์ ์ฌ์ฉํ์ฌ ์ ์ํจ. spring boot.
- ๋ฐฑํฑ์ด ์์ฑ๋จ('')
- ์คํธ๋ง ์ดํฐํด๋ ์ด์ ์ด ๊ฐ๋ฅํจ. '๋ด ๋์ด๋ ${this.age} ์ ๋๋ค'
- ๊ฐ์ฒด๋ฅผ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ : ์๋ ์์ 1๋ฒ๊ณผ 2๋ฒ์ ๋์ผํ ๊ฒฐ๊ณผ
test(first: string, second:string): any {
// 1๋ฒ
// const result = {
// first: first,
// second: second
// }
// return result;
// 2๋ฒ
return {first, second};
}
- ํจ์๋ฅผ value๋ก ๊ฐ๋ฅ : age, age2๋ ๊ฐ์ ๊ฒ์.
post = {
title: '์์ด์ ',
author: 'IU',
likes: 30,
age: function (a, b) {
return a + b;
},
age2 (a, b) {
return a+b;
}
}
test2(first: string, second:string):any {
// ๋ฐฐ์ด์ ํด์ฒดํด์ ๋ด๋ ๊ฒฝ์ฐ.
const a = [1,2,3];
const b = a[0];
const c = a[1];
const d = a[3];
// ์์ ์ฝ๋๋ฅผ ์๋์ ๊ฐ์ด ๋ณ๊ฒฝ ๊ฐ๋ฅ
const [e,f,g] = a;
}
- ๋ฐ์ดํฐ์ ๋ฉ์๋๋ก ์ด๋ฃจ์ด์ง
- ์๋ฐ์คํฌ๋ฆฝํธ๋ ๋น๋๊ธฐ ์ฒ๋ฆฌ๋ฅผ ์ํด ์ฝ๋ฐฑ ํจ์๋ฅผ ์ฌ์ฉํจ.
- ์ฝ๋ฐฑ ํจํด์ ๊ฐ๋ ์ฑ์ด ๋์๊ณ ๋น๋๊ธฐ ์ฒ๋ฆฌ ์ค ๋ฐ์ํ ์๋ฌ์ ์์ธ์ฒ๋ฆฌ๊ฐ ๊ณค๋
- ํธ๋์คํ์ผ๋ฌ
- ๋ชจ๋ ๋ฒ๋ค๋ฌ
- TypeScript > ES6 > ES5
- ๋ฐ์ฝ๋ ์ดํฐ ํํ(๋๋ ์ด๋ ธํ ์ด์ )์ผ๋ก ์ค์ ํ ์ ์์.
- ํด๋๊ตฌ์กฐ
- main.ts
import { enableProdMode } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app/app.module';
import { environment } from './environments/environment';
if (environment.production) { // ๊ฐ๋ฐ/์์ฉ ๊ตฌ๋ถ ์ฌ๋ถ
enableProdMode();
}
platformBrowserDynamic().bootstrapModule(AppModule)
.catch(err => console.error(err));
- polyfills.ts
- ํฌ๋ก์ค ๋ธ๋ผ์ฐ์ง์ ์ํ ์นํ์ค์ ์ ๊ณต ์ํฌํธ ์ญํ .
- app.module.ts
- App์์ ์ฌ์ฉํ๋ ์ฝคํฌ๋ํธ ๋ก๋ฉ ๋ฐ ์ ์
์ปดํฌ๋ํธ๋ ํ๋ฉด์ด๋ค.
- typescript๋ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ง๊ณ ์์ผ๋ฉฐ, viewํ์ด์ง์์ {{ }} ํํ๋ ๋ฐ์ดํฐ๊ฐ ๋ฐ์ธ๋ฉ ๋์ด ์๋ ๊ฒ์.
- ์ปดํฌ๋ํธ๊ฐ ๋ฐ์ดํฐ๋ฅผ ์ฃผ๊ณ ๋ฐ๋ ๋ฐฉ๋ฒ.
- ๋ฐ์ดํฐ๋ฐ์ธ๋ฉ์ ํ ํ๋ฆฟํ ์ด๋ผ ํจ.
- {{expression}}
- [property]="expression"
- (event)="handler"
- [(ngModel)]="property"
<img [src]="imgSrc">
export class AppComponent {
imgSrc = '/assets/IU.jpg'
}
<a (click)='btnSiginClicked()'>Sign In</a>
export class AppComponent {
title = 'ํ์ํฉ๋๋ค.'
name = '์ด๋ฐด์ ์ค'
age = 30
myString = `Hello! ${this.name}.`
btnSiginClicked() {
this.myString = '์์ด์ ';
}
}
- ๋ฐ์ดํฐ๊ฐ ๋ฐ๋์ด๋ ํ๋ฉด์ด ๋ฐ๋๊ณ ํ๋ฉด์ด ๋ฐ๋์ด๋ ๋ฐ์ดํฐ๊ฐ ๋ฐ๋๋ ๊ฒ.
- FormsModule๋ฅผ module.ts ํ์ผ์ ์ถ๊ฐํด์ผ ํจ.
import { FormsModule } from '@angular/forms';
@NgModule({
imports: [
FormsModule
],
})
<input type='text' [(ngModel)]='initInputString'>
<span>{{initInputString}}</span>
export class AppComponent {
initInputString = '์ด๋ฆ์ ์
๋ ฅํ์ธ์.'
}
- input text์ ๊ฐ์ ์ ๋ ฅํ๋ฉด span ์์ {{initInputString}} ๊ฐ๋ ํจ๊ป ๋ณ๊ฒฝ๋จ.
- ๋ฐ๋ณตํด์ ๋ณด์ฌ์ง๋ ๋ฐ์ดํฐ
- ngFor
- ngIf
- ๋ถ๋ชจ๊ฐ ์์์๊ฒ๋ง ๊ณต์ ํ ์ ์์.
- ํ ์๋ฒ์ง๊ฐ ์์์๊ฒ ์ฃผ๋ ค๋ฉด, ํ ์๋ฒ์ง - ์๋ฒ์ง - ์์ ์๊ฒ ์ ๋ฌํด์ผ ํจ.
- ์์์ ๋ถ๋ชจ์๊ฒ๋ง ์ ๋ฌํ ์ ์์.
- Service ์ฒ๋ฆฌํจ
- ๋ถ๋ชจ๊ฐ ์์์ ํธ์ถํ๋ html์ [์์์ด ๋ฐ์ ๋ณ์๋ช ]='๋ถ๋ชจ ์ปดํฌ๋ํธ์ ์ ์๋ ๋๊ธธ ๊ฐ'
<app-content [colors]='objFavoriteColors'></app-content>
- ์์์ components.ts ํ์ผ์ ์ ์ํจ
export class ContentComponent implements OnInit {
@Input()
colors:any = {}
}
- ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ง๊ณ ์๋ ์ปดํฌ๋ํธ
- ๊ทธ๋ ๊ทธ๋ ๋ค๋ฅด๋ค
- ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ง๊ณ ์์ง ์๋ ์ปดํฌ๋ํธ
- ์ฌ์ฌ์ฉ ์ปดํฌ๋ํธ. ์ ํธ๋ฆฌ๋ทฐํธ๋ฅผ customํ๊ฒ ๋ง๋ค์ด์ ์ฌ์ฌ์ฉ ๊ฐ๋ฅํจ.
- ์ดํธ๋ฆฝ๋ทฐํธ ๋๋ ํฐ๋ธ
- ๊ตฌ์กฐ ๋๋ ํฐ๋ธ : ngFor, ngIf ๋ฅผ ํตํด์ DOM ๋ ์ด์์ ๋ณ๊ฒฝ
- ์ปดํฌ๋ํธ ๋๋ ํฐ๋ธ
D:\js-demo\MyStore>ng g directive textBlue
CREATE src/app/text-blue.directive.spec.ts (233 bytes)
CREATE src/app/text-blue.directive.ts (145 bytes)
UPDATE src/app/app.module.ts (897 bytes)
- ์๋์ ๊ฐ์ text-blue.directive.ts ํ์ผ ์์ฑ๋๊ณ , app.module.ts ์ ์๋ ์ถ๊ฐ๋จ.
import { Directive, ElementRef } from '@angular/core';
@Directive({
selector: '[textBlue]'
})
export class TextBlueDirective {
// ๊ธ์์์ ํ๋์ผ๋ก ๋ณ๊ฒฝ
constructor(el: ElementRef) {
el.nativeElement.style.color = 'blue';
}
}
- ํ ํ๋ฆฟ์ ์๋์ ๊ฐ์ด ์ถ๊ฐํ๋ฉด ์กฐํ์ ํ๋๊ธ์ด ์กฐํ๋จ
<p textBlue>textBlue directive</p>
๋ฐ์ดํฐ ์์ฒด๋ฅผ ๋ณ๊ฒฝํ๋ ๊ฒ์ ๋ถ์ ํจ๊ณผ๊ฐ ์์ผ๋ฏ๋ก ํ๋ฉด์ ํ์ํ๋ ํ์๋ง ๋ณ๊ฒฝํ๊ณ ์ถ์ ๋ ์ฌ์ฉํ๋ ๊ฒ์ด ํ์ดํ ์ด๋ค.
- Angular Pipe API List (https://angular.io/api#api-list)
- ์ฌ๋ฌ ๊ฐ์ ํ์ดํ๋ฅผ ์กฐํฉํ์ฌ ๊ฒฐ๊ณผ๋ฅผ ์ถ๋ ฅํ๋ ๊ฒ์ '์ฒด์ด๋ ํ์ดํ' ๋ผํจ.
- ์ปดํฌ๋ํธ์ ๋๋ ํฐ๋ธ๋ ์๋ช ์ฃผ๊ธฐ๋ฅผ ๊ฐ์ง๋ฉฐ, ์๋ช ์ฃผ๊ธฐ ์ด๋ฆ ์์ ng๊ฐ ๋ถ์ ์๋ช ์ฃผ๊ธฐ ํ ๋ฉ์๋๋ฅผ ์ ๊ณตํ๋ค.
- ๋ผ์ดํ์ธ์ดํด ํจ์ ์คํ ์์
- ์๋ฐ์คํฌ๋ฆฝํธ๋ On* : ์ด๋ฒคํธ์ ๊ด๋ จ๋ ์ฝ๋ฐฑ ํจ์์ธ ๊ฒฝ์ฐ๊ฐ ๋๋ถ๋ถ์
- ์ธํฐํ์ด์ค์ ํํ๋ก ์ ๊ณต๋จ.
- cf) mutable & immutable (์ค๋ช )
- ์ปดํฌ๋ํธ๋ ํ๋ฉด์ ๊ตฌ์ฑํ๋ ๋ทฐ๋ฅผ ์์ฑํ๊ณ ๊ด๋ฆฌํ๋ ๊ฒ์ด ์ฃผ๋ ์ญํ .
- ์ปดํฌ๋ํธ์ ์ฃผ์ ๊ด์ฌ์ฌ ์ด์ธ์ ๋ถ๊ฐ์ ์ธ ๊ธฐ๋ฅ์ ์ฝ๋ ์ค๋ณต๋๊ณ ์ฌ์ฌ์ฉ์ฑ์ด ๋ฎ์์ง๊ณ ๋ณต์ก๋๋ ์ฌ๋ผ๊ฐ.
- ์ ํ๋ฆฌ์ผ์ด์ ์ ์ญ์ ๊ด์ฌ์ฌ๋ฅผ ๋ถ๋ฆฌ ํ๋ ๊ฒ์ด ํ์. ==> ๊ธฐ๋ฅ์ ์๋น์ค๋ก ๋ถ๋ฆฌ ๊ตฌ์ฑ
- DI : ์์กด๊ด๊ณ๊ฐ ์์ค์ฝ๋ ๋ด๋ถ๊ฐ ์๋ ์ธ๋ถ์ ์ค์ ์์ ์ ์๋๊ฒ ํ๋ ๋์์ธ ํจํด ์ค์ ํ๋.
- ์ฐพ์๋ณผ๊น? google : dependency injection
- ์๋น์ค ์์ฑ
- article.service.ts ํ์ผ์ด ์์ฑ.
import { Injectable } from '@angular/core';
**@Injectable**({
providedIn: 'root'
})
export class ArticleService{
getArticles():Array<any> {
return [
{title: 'Angular', content: 'Angular Study', author: 'lee', likes: 30},
{title: 'Google', content: 'Google Cloud', author: 'gee', likes: 40},
{title: 'Vue.js', content: 'Vue.js Study', author: 'yu', likes: 50},
{title: 'Mom', content: 'Mom Study', author: 'lee', likes: 60},
{title: 'Java8', content: 'java ramda Study', author: 'jee', likes: 70}
]
}
}
export class AppComponent implements OnInit {
// ์์ฑ์ ์ญํ ์ ์๋น์ค ์ด๊ธฐํ
constructor( private articleService: ArticleService) {
}
}
<h1>์ฑ
๋ชฉ๋ก</h1>
<ul>
<li *ngFor='let article of objArticles'>
{{article.title}} by {{article.author}}
</li>
</ul>
- ์๋ธ์ ํ์ผ์ providedIn: 'root' ๋ผ๋ ์ ์๋ angular6๋ถํฐ ์์ฑ๋จ.
- ์ด์ ์๋ app.module.ts ํ์ผ์ NgModule.providers ๋ผ๋ ๋ถ๋ถ์ ์ ์ํด์ ์ฌ์ฉํจ.
- ๋๋ฝ์ด ๋ฐ์ํ์ฌ ์๋น์ค์ ์ ์ํจ.
- root? ์ปดํฌ๋ํธ์ root. ํ๋ฉด์ root๋ฅผ ์๋ฏธํจ.
- ํน์ ์ปดํฌ๋ํธ๋ก ์ ์ฝํ ์ ์์.
- ์๋น์ค๋ฅผ ์ปดํฌ๋ํธ ๊ฐ ๋ฐ์ดํฐ ์ค์ฌ์๋ก ์ฌ์ฉํ๋ฉด ์ผ์ ํ ํ์์ ์๋ฃ๋ฅผ ์ฌ์ฉํ์ฌ ์ปดํฌ๋ํธ ๊ฐ์ ์ํ ๊ณต์ ๊ฐ๋ฅ.
-
Reactive ํ๋ก๊ทธ๋๋ฐ ... ์๋ฅผ ๋ค์ด ์ค๋ช
ํ๋ฉด...
- ์ดํฐ๋ ์ดํฐ ํจํด : ๋ฐ์ดํฐ ์ค๋น์ ์ฌ์ฉ์ ๊ณผ์ ์ ๋ถ๋ฆฌํจ. ๋ฐ์ดํฐ๋ฅผ ์ฌ์ฉํ๋ ๊ณผ์ ์์ pull ํ๋ ๋ฐฉ๋ฒ์ผ๋ก ์ฌ์ฉ.
- ์ต์ ๋ฒ ํจํด : ๋ฐ์ดํฐ๋ฅผ ์ฌ์ฉํ ๋ push ํ์ฌ ์ฌ์ฉ.
- ์ต์ ๋ฒ ํจํด์ ๊ณ์นํ๊ฒ์.
- ๋ฐ์ดํฐ ์ ์ก์ ๋๋ฌ๋ค๋ผ๋ ๋ฉ์์ง๋ ํจ๊ป ์ ์กํจ.
- ๋ฐ์ดํฐ๋ฅผ ์ ์กํ๋ ํํธ๊ฐ ์ค์ฌ์ด ๋์ด์ ์ฒ๋ฆฌํด์ผ ํจ.
- Rx (Reactive Extention) : ReactiveX
- reactive ํ๋ก๊ทธ๋จ์ ํ์ค.
- reactive-streams.org
- java 9, spring 5
- ๋ฐ์ดํฐ๋ฅผ ๋ง๋ธ๋ค์ด์ด๊ทธ๋จ ์ผ๋ก ์๊ฐ์ ํ๋ฆ์ผ๋ก ์ฒ๋ฆฌํ๋ค. (๋ง๋ธ ๋ค์ด์ด๊ทธ๋จ์ ํตํด ์ด ์ฐ์ฐ์๋ค์ ๋ํด ์ค๋ช )
- reactive ๋ฐฉ์์ผ๋ก ๊ฐ๋ฐํด์ผ ํจ.
- ์์ฐ์์ ์๋น์๋ก ๋ถ๋ฆฌ
- ๋ฐ์ดํฐ๋ push ๋ฐฉ์ (emit ๋ฐฉ์ถํ๋ค)
- ๋ฐ์ดํฐ ์์ฐ์ data emit ๋ ๊ฒ์ provider or observer
- ๋ฐ์ดํฐ ์๋น์ consumer
- RxJS
- observable / observer
- subscriber
- ๋๋ถ๋ถ์ด Cold Observer
- ์ต์ ๋ฒ๋ธ์ ์์ฑ, ๋ณํ, ํํฐ๋ง, ์๋ฌ์ฒ๋ฆฌ์ ๊ธฐ๋ฅ์ ํ๋ ํจ์.
- ๋ฐ์๋ ๊ฐ๊ณตํ๋ฉด์ ๋ฐ์ดํฐ๋ฅผ ๋ฐ์ ์ ์์.
- subscribe ๋ฉ์๋์ ๋๋ฌํ๊ธฐ ์ ๊น์ง ์ฒด์ด๋์ ํตํด ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌํ ์ ์์.
-
HttpClient
- ์นํ์ค์ fetch์ด์ง๋ง angular๋ HttpClient ์ด๋ค.
- Angular4.3 ์ดํ์ ์ถ๊ฐ๋จ.
- ์๋น์คํํ. ์ฃผ์ ๋ฐ์ ์ฌ์ฉํจ.
- ์๋น์ค ํ์ด์ง์ ์๋ ๋ด์ฉ ์ถ๊ฐ
export class ArticleService{
constructor( private http: HttpClient) {}
fetchDataTest() {
const html = this.http.get('http://www.google.com');
}
...
- http ์ฌ์ฉ ๋ฐฉ์
- ๋๊ธฐ์ : const html = this.http.get('http://www.google.com');
- ๋น๋๊ธฐ์ : ์์ฒญํ๊ณ ์๋ตํ๊ณ ์ฝ๋๊ฐ ์ค๋ ๋์ ๊ธฐ๋ค๋ฆฌ์ง ์์. subscribe(์ฝ๋ฐฑํจ์) .
fetchDataTest() {
this.http.get('https://api.github.com/')
.subscribe(function(response) { // ์๋ต์ด ์ค๋ฉด ์ฝ๋ฐฑ๋๋ ํจ์
console.log(response);
});
}
// ์ฌ์ฉํ ๋
this.articleService.fetchDataTest();
- ์ฃผ์ : endpoint
- ๋ฐ์ดํฐ ์๋ต ์คํ
- Back-end๋ endpoint์ data spec ์ ์ ์ํ๋ฉด๋จ.
- endpoint๋ API ์ฃผ์
- API ์ค๋ช ์๊ฐ ํ์ํ๋ฉฐ Front End๋ Mock ์๋ฒ๋ฅผ ํตํด์ ํ ์คํธ ํ ์ ์์.
- ์ด๋ฐ์์ Arch.๋ฅผ SOA ๋ผ๊ณ ๋ ํจ.
export class ArticleService{
constructor( private http: HttpClient) {}
fetchDataTest():Observable<any> {
return this.http.get('https://api.github.com/users/HiroSung');
}
...
// ์ฌ์ฉํ ๋
this.articleService.fetchDataTest()
.subscribe(function(response) { // ์๋ต์ด ์ค๋ฉด ์ฝ๋ฐฑ๋๋ ํจ์
console.log(response);
});
- ๋ ๋ค๋ฅธ ์ฌ์ฉ๋ฒ
this.articleService.fetchDataTest()
.subscribe( res => { // ์๋ต์ด ์ค๋ฉด ์ฝ๋ฐฑ๋๋ ํจ์
console.log(res);
this.gitInfo = res;
});
- Restful API ์๋น์ค
- ์๋น์ค์ Format์ ์ด๋ป๊ฒ ๊ฐ์ ธ๊ฐ ๊ฒ์ธ์ง ํ์
- API ์์ฒญ ๋งค๋ด์ผ
-
๋ค์ด๋ฒ Developer
- ๋ค์ด๋ฒํค : clientID - 7Nfau_qhtlKWt9Duksod, Client Secret - S77xhQWZUl
- -H: header์ ์ค์ ํ ์ ๋ณด
-
๋ค์ด๋ฒ Developer
- NewsAPI. API Key : 98180e15767742d79e166b95166dd7b6
curl "https://openapi.naver.com/v1/search/news.xml?query=%EC%A3%BC%EC%8B%9D&display=10&start=1&sort=sim" \
-H "X-Naver-Client-Id: {์ ํ๋ฆฌ์ผ์ด์
๋ฑ๋ก ์ ๋ฐ๊ธ๋ฐ์ client id ๊ฐ}" \
-H "X-Naver-Client-Secret: {์ ํ๋ฆฌ์ผ์ด์
๋ฑ๋ก ์ ๋ฐ๊ธ๋ฐ์ client secret ๊ฐ}" -v
- ๊ณต๊ณต๋ฐ์ดํฐํฌํธ : https://www.data.go.kr/
- ๋ง๋ธ : https://developer.marvel.com/docs#!/
- URI : ํ๋ผ๋ฏธํฐ๊ฐ์ง ํฌํจ๋ ์ ๋ณด
getNews():Observable<any> {
const params = new HttpParams()
.set('country', 'kr')
.set('apiKey', '98180e15767742d79e166b95166dd7b6')
return this.http.get('https://newsapi.org/v2/top-headlines', {params});
}
- DOM์ ์ง์ handling ํ๋๋ก ํ์์.
- ํผ์ ์ง์ ์ปจํธ๋กค
- FormBuilder๋ฅผ ์์ฑ์์ ์ฃผ์ ํจ.
- angular๊ฐ ์ ์ผ ์๋์ด ์์
- URL์ ์ฃผ์๋ฅผ ๋ณ๊ฒฝํด ์ฃผ๋๊ฒ.
- SPA ์์ ๋ผ์ฐํ ์ ์ฌ์ฉํจ.
- ์ด๊ธฐ ๋ก๋ฉ ์๋๊ฐ ์ค์ํจ.
- SEO ๋ฌธ์ : index๋ ๋ฐ์ดํฐ๊ฐ ์๊ธฐ ๋๋ฌธ์ ๊ฒ์์์ง์์ ๊ฒ์์ด ์๋จ.
- Angular2 - 4(์ถ๋ฐ) - 5 - 6(์ธ๋งํจ) - 8(์์ ํ)
- '#' ์ดํ์ URI ๋ Client side script ์ด๋ค
- ๋ณด์์ ์ ์ฉ ๊ฐ๋ฅ
- [์น ํ๋ก ํธ์๋ ๊ธฐ์ ์คํ ์ ํ] (https://ingeec.tistory.com/107)
- [Angular ์ ์ - ์ด์ ๋ชจ. ํฌ์ด์๋ง์น] (https://poiemaweb.com)
- Angular F/B ๋ํธํ - ๋ฆฌ์ฟ ๋ฃจํ .
- ์ฌ๋ดํฌํ์์ ์ฌ์ฉํ ์ ์์์ง๋ ๊ณ ๋ฏผํด์ผ ํจ. Angular๋ SPA ๋ชจ๋์ด๋ค.
- StackBlitz
- TypeScript. - "vue.js typescript", "angular typescript"
- Angular.kr ์ ๋ฐ๋ผํ๊ธฐ๋ฅผ ๋ชจ๋ ํด๋ณด๊ธฐ๋ฅผ ๊ถ์ฅ.
HuCloud ๊น์๊ณค ๊ฐ์ฌ. 1/20~22. ํ๊ตญ์ ๋ณด๊ธฐ์ ์.