기본 콘텐츠로 건너뛰기

[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

댓글

이 블로그의 인기 게시물

[django] django rest framework 로그인 과정 | 장고 로그인 | 인증...

[django] django rest framework 로그인 과정 | 장고 로그인 | 인증... django 는 기능이 참 너무 많다 ^^; 지금은 서버는 django로, 프론트는 angular를 붙여서 간단한 웹을 만들어 보려고 한다. 웹 만들때 항상 회원가입/로그인 기능은 맨 앞에 구현한다. 어떻게 구현하면 좋을까... 찾아보다가 이 기능을 구현할 수 있는 방법이 너무 많아서 정보를 찾기 더 어려웠다. 일단 나는 django에서 django rest framework라는 것을 사용해서 API를 만드려고 한다. 순수 django 튜토리얼에는 바로 template 랑 연결해서 설명하는 부분이 많았다. 나는 그냥 API 만 만들고 싶다고!! 그래서 찾은 것이 django REST framework. https://www.django-rest-framework.org/api-guide/authentication django REST framework 설치 using pip pip install djangorestframework settings.py INSTALLED APPS 에 추가해야함 INSTALLED_APPS = [ ... 'rest_framework', ] django REST framework 에서도 인증 관련해서 제공하는 것이 1개가 아닌 여러 개다. 나는 그중에 TokenAuthentication을 이용해서 로그인을 구현해 보려고 한다. TokenAuthentication Token authentication is appropriate for client-server setups, such as native desktop and mobile clients. 이렇게 나와있어서 내가 하려는 것과 일치해서 이걸로 결정 ~ 솔직히 처음 로그인을 구현하려고 하면 도대체 그 과정이 어떻게 되는지 모를 수 도 있다. 나는 쉽게 정리하면 아래와 같은 과정이라고 생각한다. 로그인 로그아웃...

(주)레터플라이 채용 정보: 프로그래밍을 생각하면 가슴이 뛰는 개발자...

(주)레터플라이 채용 정보: 프로그래밍을 생각하면 가슴이 뛰는 개발자... Angular.js, Python, MySQL 중 한 가지 언어에 뛰어나신 분도 좋고 개발 업무 전반적으로 센스가 있으신 분도 환영합니다. 맡은 업무를 성실하게 수행해 나갈 수 있는 책임감과 태도를 갖고계신 분, 그리고 항상 새로운 방법론에 도전하고 포기를 모르는 분일수록 저희와 더욱 잘 맞을 것 같습니다. Angular.js, Python, MySQL 중 한 가지 언어에 뛰어나신 분도 좋고 개발 업무 전반적으로 센스가 있으신 분도 환영합니다. 팀 내 뛰어난 풀스택 개발자분들이 Angular.js, Python, MySQL 모두 작업 가능하시니 오셔서 함께 배우며 즐겁게 작업하시면 됩니다. 맡은 업무를 성실하게 수행해 나갈 수 있는 책임감과 태도를 갖고계신 분, 그리고 항상 새로운 방법론에 도전하고 포기를 모르는 분일수록 저희와 더욱 잘 맞을 것 같습니다. 개발 업무: 레터플라이의 핵심 기능인 편지, 사진을 제작하는 레터에디터, 포토에디터 개발. 이 기능들은 "모바일 웹을 통한 출력제품 생산 자동화 기술"(특허 출원 준비중)로서 레터플라이에서 자체개발했습니다. 근무 지역: 광화문역 5번출구 바로 앞 근무 환경: 책임과 존중을 중요시하는 수평적인 분위기, 도전적이며 서로에게 배우는 문화 근무 시간: 10-19시, 출근시간 자유 지정. 급여: 연봉/스톡옵션 협의 지원 방법: 팀 지원하기 더 많은 내용은 더 많은 내용은 더팀스 에서 확인하세요! from http://theteams.tistory.com/721 by ccl(A) rewrite - 2020-03-20 09:20:18

jqxGrid 정렬, 필터 메뉴 숨기기

jqxGrid 정렬, 필터 메뉴 숨기기 How I can remove filter to particular grid column - Angular, Vue, React, Web Components, Javascript, HTML5 Widgets Hi, I tried that it's working. I set properties to those columns as sortable: false, filterable: false. but when I clicked on the column one drop down is appearing with options "sort ascending", "sort descending", "remove sort" and those are all in disable www.jqwidgets.com from http://devesim.tistory.com/90 by ccl(A) rewrite - 2020-03-11 04:20:29