Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Angular2に入門した
Search
y-ohgi
October 03, 2016
Programming
76
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Angular2に入門した
y-ohgi
October 03, 2016
More Decks by y-ohgi
See All by y-ohgi
ランチタイムLT会3周年!ランチタイムLT会を3年間続けられたお話
y0hgi
1
130
EKS Auto-Mode with Kro
y0hgi
0
250
AWS Cloud Control API & AWSCC Provider
y0hgi
1
83
AWSとSRE 〜サービスの信頼性〜
y0hgi
2
390
re:Invent 2024 re:Cap コンピューティング&コンテナ
y0hgi
3
490
クラウドを今から学ぶには
y0hgi
0
740
クラウド・コンテナ・CI/CDわからん会
y0hgi
0
88
入門 Docker - JAWS-UG東京 ランチタイムLT会 #14
y0hgi
1
450
AWS CloudShell で開発したかった話 / i-cant-develop-in-cloudshell
y0hgi
1
2.1k
Other Decks in Programming
See All in Programming
AIを活用したE2Eテスト実装効率化のあゆみ / ebisu-mobile-14-kotetu
kotetuco
0
160
AI時代の仕事技芸論〜ソフトウェア開発で「遊ぶように働く」職人的熟達のすすめ(スクフェス仙台 2026バージョン)
kuranuki
0
520
TypeScript+Orvalで実現する型安全かつ堅牢でスケーラブルなマルチチャネル通知基盤 / TSKaigi Night talks ~after conference~
d0riven
0
390
関数型プログラミングのメリットって何だろう?
wanko_it
0
150
Hunting Vulnerabilities in Symfony with LLMs
vinceamstoutz
0
580
コーディングルールの鮮度を保ちたい for SRE NEXT 2026 / keep-fresh-go-internal-conventions-sre-next-2026
handlename
0
130
共通化で考えるべきは、実装より公開する型だった
codeegg
0
150
IBM Bobを活用したレガシーアプリの最新化
oniak3ibm
PRO
1
240
OSINT for SRE: 学術論文とポストモーテムから探る システム障害の共通パターン / SRE NEXT 2026
tomoyk
1
3k
吝嗇家のためのAI活用 / AI development for miser - ChatGPT + Issue Driven Development
tooppoo
0
180
PHPだって関数型したい 〜できること、できないこと〜 / fp-in-php
jsoizo
0
170
決定論的オーケストレーションの設計と実装 / Design and Implementation of Deterministic Orchestration
nrslib
4
1.6k
Featured
See All Featured
Tips & Tricks on How to Get Your First Job In Tech
honzajavorek
1
560
Rails Girls Zürich Keynote
gr2m
96
14k
Visualization
eitanlees
152
17k
My Coaching Mixtape
mlcsv
0
170
Designing for Performance
lara
611
70k
Unsuck your backbone
ammeep
672
58k
GitHub's CSS Performance
jonrohan
1033
470k
Building a A Zero-Code AI SEO Workflow
portentint
PRO
0
630
Chasing Engaging Ingredients in Design
codingconduct
0
230
How to Align SEO within the Product Triangle To Get Buy-In & Support - #RIMC
aleyda
2
1.6k
Ruling the World: When Life Gets Gamed
codingconduct
0
280
Highjacked: Video Game Concept Design
rkendrick25
PRO
1
400
Transcript
Angular2に⼊⾨した話 ~ WebAPI(Rails5)を使った todoアプリを作る ~
やること 1. ⾃⼰紹介 2. ⽬標 3. デモ 4. Rails5 5.
Angular2 6. 最後に
⾃⼰紹介 ▪ ⼤⽊ 裕介 ▪ 株式会社オクト インターン⽣ /学⽣(21) ▪ PHP/Ruby
on Rails/jQuery ▪ Angular2 歴 5⽇ ▪ 初LT
⽬標 ▪ 初学者さんに雰囲気を知ってもらう – スライドを持ち帰って⼊⾨してもらう – 初⼼者がRails5とAngular2でtodoアプリを作った過程を紹介
demo https://github.com/y-ohgi/todo_app
環境 $ node –v # v6.2.2 $ npm –v #
3.9.5 $ ruby –v # 2.2.5 $ rails –v # 5.0.0.1
Ruby on Rails
Rails5 ~プロジェクトの作成からscaffold~ $ rails new backend --api # apiモードでプロジェクトの作成 $
cd backend $ bundle install --path vendor/bundler # scaffoldの作成 $ rails g scaffold Todo title:string body:string $ rails db:migrate
Rails5 ~CORS対応~ $ backend/Gemfile gem 'rack-cors' # gem ’rack-cros' $
backend/config/initializers/cors.rb Rails.application.config.middleware.insert_befo re 0, Rack::Cors do allow do origins ‘*’ # ここで許可するドメインを指定! resource '*', headers: :any, methods: [:get, :post, :put, :patch, :delete, :options, :head] end end #Rails.application.config.middleware.insert_ before 0, Rack::Cors do # allow do # origins ’example.com’ # resource '*', # headers: :any, # methods: [:get, :post, :put, :patch, :delete, :options, :head] # end #end
Rails5 ~APIサーバーの起動~ $ bundle install --path vendor/bundler $ rails server
# サーバーの起動!
Angular2
Angular2 TODOの全件取得と追加を実装まで
Angular2 ~プロジェクト作成~ $ npm install -g angular-cli # cliの導入 $
ng new frontend # プロジェクトの作成 $ cd frontend $ ng s # サーバーの起動
Angular2 ~プロジェクト作成~ $ npm install -g angular-cli # cliの導入 $
ng new frontend # プロジェクトの作成 $ cd frontend $ ng s # サーバーの起動
Angular2 ~今回触るファイル~ todo.ts todo.component. html todo.component.ts todo.service.ts app.component. html app.component.ts
Angular2 ~indexの編集~ todo.ts todo.component. html todo.component.ts todo.service.ts app.component. html app.component.ts
Angular2~indexの編集~ frontend/src/app/app.component.html <h1> {{title}}</h1> <!-- 追加 --> <app-todo></app-todo>
Angular2 ~今回触るファイル~ todo.ts todo.component. html todo.component.ts todo.service.ts app.component. html app.component.ts
Angular2 ~todoクラスの作成~ frontend/src/app/todo.ts # 新規作成 export class Todo{ id: string;
title: string; body: string; }
Angular2 ~serviceの作成~ todo.ts todo.component. html todo.component.ts todo.service.ts app.component. html app.component.ts
Angular2 ~serviceの作成~ $ ng g service todo create src/app/todo.service.spec.ts create
src/app/todo.service.ts import { Injectable } from '@angular/core'; @Injectable() export class TodoService { constructor() { } } $ src/app/todo.service.ts
Angular2 ~service scaffoldの編集~ ⚠ ここからソースコードがメインになります!
Angular2 ~serviceの編集(1/4)~ frontend/src/app/todo.service.ts import { Injectable } from '@angular/core'; //
追加 import { Http, Headers, RequestOptions, Response } from '@angular/http'; import 'rxjs/add/operator/map'; import 'rxjs/add/operator/switchMap'; import 'rxjs/add/operator/toPromise'; import 'rxjs/add/operator/find'; import { Todo } from './todo';
Angular2 ~serviceの編集(2/4)~ @Injectable() export class TodoService { private url: string
= 'http://localhost:3000/todos'; constructor(private http: Http) { } // 全件取得 fetchAll() { } // todo追加 postTodo() { } }
Angular2 ~serviceの編集(3/4)~ // 全件取得 fetchAll(): Promise<Todo[]> { return Promise.resolve( this.http.get(this.url).toPromise()
.then(res =>res.json().map(todo => <Todo[]> todo)) ); }
Angular2 ~serviceの編集(4/4)~ // todo追加 postTodo(title: string, body: string): Promise<Todo>{ let
params = JSON.stringify({ 'todo': { 'title': title, 'body': body } }); let headers = new Headers({ 'Content-Type': 'application/json' }); let options = new RequestOptions({ headers: headers }); return this.http.post(this.url, params, options).toPromise() .then(res => res.json() as Todo); }
Angular2 ~ componentの作成~ todo.ts todo.component. html todo.component.ts todo.service.ts app.component. html
app.component.ts
Angular2 ~ componentの作成~ $ ng g component todo create src/app/todo/todo.component.css
create src/app/todo/todo.component.html create src/app/todo/todo.component.spec.ts create src/app/todo/todo.component.ts
Angular2 ~ componentの作成~ $ src/app/todo/todo.component.ts import { Component, OnInit }
from '@angular/core'; @Component({ selector: 'app-todo', templateUrl: './todo.component.html', styleUrls: ['./todo.component.css'] }) export class TodoComponent implements OnInit { constructor() { } ngOnInit() { } } $ src/app/todo/todo.component.html <p> todo works! </p>
Angular2 ~ todo.component.html~ todo.ts todo.component. html todo.component.ts todo.service.ts app.component. html
app.component.ts
Angular2 ~componentの編集 view~ frontend/src/app/todo/todo.component.html <h2>追加</h2> <form> <input #title placeholder="title" />
<br/> <input #body placeholder="body" /> <br/> <button (click)="postTodo(title.value, body.value)">submit</button> </form> <h2>一覧</h2> <ul> <li *ngFor="let todo of todos"> {{todo.title}}: {{todo.body}} </li> </ul>
Angular2 ~todo.component.ts~ todo.ts todo.component. html todo.component.ts todo.service.ts app.component. html app.component.ts
Angular2 ~componentの編集(1/3)~ frontend/src/app/todo/todo.component.ts import { Component, OnInit } from '@angular/core';
// 追加 import { Observable } from 'rxjs/Observable'; import { TodoService } from '../todo.service'; import { Todo } from '../todo';
Angular2 ~componentの編集(2/3)~ @Component({ selector: 'app-todo', providers: [TodoService], # 追加 templateUrl:
'./todo.component.html', styleUrls: ['./todo.component.css'] })
Angular2 ~componentの編集(3/3)~ export class TodoComponent implements OnInit { selecttodo: Todo;
todos: Todo[]; constructor(private todoService: TodoService) { } ngOnInit() { this.fetchAll(); } // 全件取得 fetchAll(){ this.todoService.fetchAll() .then((todos) => this.todos = todos); } // todo追加 postTodo(title: string, body: string){ this.todoService.postTodo(title, body) .then((todo) => this.todos.push(todo)); } }
Angular2 delete・updateはgithubで!
最後に ご静聴ありがとうございました! ツッコミお待ちしております!!!!
None