기본 콘텐츠로 건너뛰기

[Angular Tutorial 02] The Hero Editor

[Angular Tutorial 02] The Hero Editor

[Angular Tutorial 02] The Hero Editor

https://angular.io/tutorial/toh-pt1

다음 명령어를 통하여 generate 타입의 heroes 라는 이름의 component 를 생성 합니다.

ng generate component heroes

herose component가 생성 되었습니다.

컴포넌트 하위에는 css, html, ts, spec.ts 파일이 포함되어 있습니다.

app/heroes/heroes.component.ts (initial version) 파일의 구조를 살펴 봅시다.

import { Component , OnInit } from '@angular/core' ;

@ Component ({ selector: 'app-heroes' , templateUrl: './heroes.component.html' , styleUrls: [ './heroes.component.css' ] }) export class HeroesComponent implements OnInit {

constructor () { }

ngOnInit () { }

}

반드시 모든 Components 에는 Angular core library를 import 해야 합니다.

Component Class 는 반드시 Annotation 표시를 해 주어야 합니다 ex) @Component.

You always import the Component symbol from the Angular core library and annotate the component class with @Component.

@Component is a decorator function that specifies the Angular metadata for the component.

1. selector— the component's CSS element selector

3. templateUrl— the location of the component's template file.

4. styleUrls— the location of the component's private CSS styles.

heroes.component.ts (hero property) 파일에서 property 를 추가 해 보겠습니다.

export class HeroesComponent implements OnInit {

hero = 'Windstorm' ; constructor () { }

ngOnInit () { } }

그리고 heroes.component.html 파일을 다음과 같이 수정 합니다.

< p > {{hero}}

이제 src/app/app.component.html 에 view 를 추가하여 결과 화면에 출력 시켜 봅시다.

필요 없는 내용은 삭제 하였습니다.

< div style = " text-align:center" > < h1 > {{ title }} < app-heroes >

여기까지의 화면입니다.

이제 Hero 라는 Class 를 생성 해 보겠습니다.

src/app 하위에 hero.ts 라는 파일을 생성하여 다음과같이 작성 합니다.

export class Hero { id : number ; name : string ; }

src/app/heroes/heroes.component.ts 파일로 가서 hero Class 를 import 해줍니다.

그리고 HeroesComponent Class 를 다음과같이 수정합니다.

import { Component , OnInit } from '@angular/core' ; import { Hero } from '../hero' ;

@ Component ({ selector: 'app-heroes' , templateUrl: './heroes.component.html' , styleUrls: [ './heroes.component.css' ] }) export class HeroesComponent implements OnInit {

hero : Hero = { id: 1 , name: 'Windstorm' };

constructor () { }

ngOnInit () { }

}

hero 의 타입이 변경되었으니 component.html 의 출력 형식도 변경 해 주어야 겠지요?

heroes.component.html (HeroesComponent's template) 파일을 다음과 같이 수정 합니다.

※ 참고 ※

UppeCasePipe

텍스트를 모두 대문자로 변환 합니다.

사용 방법 : {{ value_expression | uppercase }}

< h2 > {{hero.name | uppercase}} Details < div >< span > id: {{hero.id}} < div >< span > name: {{hero.name}}

{{hero.name | uppercase}}의 내용 WINDSTORM 은 대문자로 출력됩니다.

그리고 양방향 데이터 바인딩을 위하여 다음과같이 component.html 파일을 다시 수정합니다.

src/app/heroes/heroes.component.html (HeroesComponent's template)

< div > < label > name: < input [(ngModel)] = "hero.name" placeholder = "name" >

[(ngModel)] is Angular's two-way data binding syntax. (양방향 데이터 바인딩 문법입니다.)

하지만 ngModel 을 사용하기 위햇는 FormsModule 을 import 해주어야 합니다.

app.module.ts 파일을 열어 다음과같이 import 해줍니다.

app.module.ts (FormsModule symbol import)

상단에 import 를 해 준 다음에는 NgModule 의 import 부분에 이름을 꼭 넣어주어야 합니다.

그리고 app.module.ts 파일을 살펴보면 CLI를 통해서 생성한 heroes.component 가 자동으로 선언 되어있는것을 볼 수있습니다.

물론 declarations 에도 선언되어 있습니다. CLI 를 사용할 때 편리한 장점 중 하나입니다.

import { BrowserModule } from '@angular/platform-browser' ; import { NgModule } from '@angular/core' ; import { FormsModule } from '@angular/forms' ; // <-- NgModel lives here import { AppComponent } from './app.component' ; import { HeroesComponent } from './heroes/heroes.component' ;

@ NgModule ({ declarations: [ AppComponent , HeroesComponent ], imports: [ BrowserModule , FormsModule // FomsModule 추가 ], providers: [], bootstrap: [ AppComponent ] }) export class AppModule { }

여기까지의 모습입니다.

You used the CLI to create a second HeroesComponent.

You displayed the HeroesComponent by adding it to the AppComponent shell.

You applied the UppercasePipe to format the name.

You used two-way data binding with the ngModel directive.

You learned about the AppModule.

You imported the FormsModule in the AppModule so that Angular would recognize and apply the ngModel directive.

You learned the importance of declaring components in the AppModule and appreciated that the CLI declared it for you.

CLI 를 이용하여 새로운 HeroesComponent 를 생성 하였습니다. HeroesComponent 를 AppComponent 에 추가하여 출력 하였습니다. UppercasePipe 를 이용하여 문자열을 대문자로 바꾸어 출력 해보았습니다. 양방향 데이터 바인딩 ngModel 을 선언 하였습니다.

from http://nina-life.tistory.com/37 by ccl(A) rewrite - 2020-03-06 14:20:41

댓글

이 블로그의 인기 게시물

[020] 파동함수를 쓰기

[020] 파동함수를 쓰기 [020] 파동함수를 쓰기 [020] 파동함수를 알면 우리가 원하는 뭔가를 알 수 있다고 했는데, 정작 파동을 함수로 쓰는 것은 아직 다루지 않았고, 양자역학 내용을 좀 더 진행하려면 왜 파동이 삼각함수로 써 지는지를 한번은 정리를 해야겠다. 수학이 많이 나올 예정이다. 앞에서 파동함수를 아래 형태로 쓸 수 있다고 했었는데, 왜 이런 형태가 되는지 알아보자. [020-01] 파동함수 사인파의 일반형은 코사인 (cos) 을 이용해 쓰지만, 사인 (sin) 과 코사인은 위상차이만 있는 함수들이고, 우리는 앞으로 사인을 이용해 문제를 풀 예정이라 사인을 이용하기로 한다. 파동함수는 양자역학에서 갑자기 나온 말이 아니고, 원래 파동을 함수의 형태로 쓴 것을 파동함수라고 부른다. 파동-입자 이중성을 가지니까 기존에 파동을 함수의 형태로 쓰던 그 모양을 가져다 쓴 것. 주교재의 16장에 있는 내용이다. [020-02] 펄스 - Pulse 아래와 같이 하나의 진동이 왼쪽에서 오른쪽으로 진행하는 상황을 생각해보자. 시간 t 일 때, 위치 x 의 밧줄의 높이를 y 라 하고, 셋의 관계를 기호로 쓰면, 그러면 아래의 관계가 성립한다. 이게 무슨말이냐면, 시간 t 일 때 위치 x 의 밧줄의 높이 와 시간 0 일 때 위치 x-vt 의 밧줄의 높이가 같다 는 말인데, 그림으로 보면, 왼쪽은 시간 0, 오른쪽은 시간 t 이고, t 일 때 P 의 높이와 0 일 때 높이가 같다는 것. [020-03] 파동을 식으로 써보자. 아래의 파동을 보자. 지금 내용부터는 사인이든 코사인이든 상관없는데, 위 그림이 사인이니까 아래의 식 으로 쓸 수 있다. A는 진폭이고, 주기, 진동수, 파장 같은 값을 아직 모르니까 a 는 아직 정해지지 않은 값 이다. 일반적으로 알고있는 삼각함수는 가로축이 각도 이지만, 우리는 공간을 진행하고 있는 파동을 다루니까 위의 형태로 써진다고 생각하자. x=0 에서 진폭이 ...

(주)편두리 채용 정보: PYUNDOORI에서 핫한 개발자분들을 모십니다.

(주)편두리 채용 정보: PYUNDOORI에서 핫한 개발자분들을 모십니다. - 좋아요 아래와 같은 업무를 훌륭히 감당하실 분을 모십니다.서비스 관련 DB 모델링, DB 최적화 작업서버 & DB 관리Frontend & Backend 인터페이스의 최적화 작업 화합과 성장을 같이! - Angular JS의 경험 - Node.js - javascript 능숙자 - DRMS 사용 능숙자 더 많은 내용은 더 많은 내용은 더팀스 에서 확인하세요! from http://theteams.tistory.com/742 by ccl(A) rewrite - 2020-03-22 12:20:20