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
140
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
スマートグラスで並列バイブコーディング
hyshu
0
280
Generative UI & AI-Assistants for Your Angular Solutions
manfredsteyer
PRO
0
150
Semantic Version 単位で戦略を柔軟に変えて、パッケージアップデートを自動化する
daitasu
1
350
AIキャラアプリkaiwaの低遅延音声通話基盤をどう作ったか - AWS Gravitonで支える低遅延・低コストAI Agent基盤
mogamit
0
160
Mujeres en SEO Summit 2026 - Greatest Disaster Hits en Web Performance
guaca
0
240
Claude Opus 4.6以後の受託開発エンジニアの変化(Claude Code開発ノウハウ大公開スペシャルbyクラスメソッド)
iidatakuma
1
400
【やさしく解説 設計編・中級 #1】一つの車に、運転手は一人 ~ある倉庫システムの事例から~
panda728
PRO
0
130
【SRE NEXT 2026 Lunch Session】一人目専任SREの立ち上げを加速する ― AIと進めたオンボーディングで2分を0.04秒にした話
pkshadeck
PRO
0
2k
Webフレームワークの ベンチマークについて
yusukebe
0
200
Vite+ Unified Toolchain for the Web
naokihaba
0
720
Skillsは効率化、Agentsは"自分の拡張"——Builder時代のエージェント編成(CC Night 2026)
wemra
1
200
Embedded SREと共に達成した会員管理システムのAWS移行 - SRE NEXT 2026 ランチスポンサーセッション
niftycorp
PRO
0
2k
Featured
See All Featured
GitHub's CSS Performance
jonrohan
1033
470k
AI Search: Where Are We & What Can We Do About It?
aleyda
0
7.7k
StorybookのUI Testing Handbookを読んだ
zakiyama
31
6.8k
Mozcon NYC 2025: Stop Losing SEO Traffic
samtorres
1
280
The browser strikes back
jonoalderson
0
1.4k
Designing Powerful Visuals for Engaging Learning
tmiket
1
440
Tips & Tricks on How to Get Your First Job In Tech
honzajavorek
1
560
Visualization
eitanlees
152
17k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
10
1.2k
The innovator’s Mindset - Leading Through an Era of Exponential Change - McGill University 2025
jdejongh
PRO
1
220
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
12
1.7k
New Earth Scene 8
popppiees
3
2.4k
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