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
72
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
0
11
EKS Auto-Mode with Kro
y0hgi
0
240
AWS Cloud Control API & AWSCC Provider
y0hgi
1
79
AWSとSRE 〜サービスの信頼性〜
y0hgi
2
380
re:Invent 2024 re:Cap コンピューティング&コンテナ
y0hgi
3
490
クラウドを今から学ぶには
y0hgi
0
700
クラウド・コンテナ・CI/CDわからん会
y0hgi
0
84
入門 Docker - JAWS-UG東京 ランチタイムLT会 #14
y0hgi
1
440
AWS CloudShell で開発したかった話 / i-cant-develop-in-cloudshell
y0hgi
1
2.1k
Other Decks in Programming
See All in Programming
スマートグラスで並列バイブコーディング
hyshu
0
140
Language Server 使ってる? 〜VSCode と Zed の場合〜 / Are you using a Language Server? ~For VS Code and Zed~
handlename
0
780
[2026年度第1回ORセミナー] 計画最適化ベンチャーと競技プログラミング人材
terryu16
0
260
Lemonade + Foundry Toolkit でお手軽アプリ開発
seosoft
1
330
Datadog × OpenTelemetry 入門と実践のあいだ
kn_to_maxpno
1
160
LLM本来の能力を解き放つサンドボックス技術とAI民主化への適用
yukukotani
3
4k
A2UI という光を覗いてみる
satohjohn
1
140
セキュリティの専門家じゃなくてもできる。「セキュリティ意識」をアップデートして サプライチェーン攻撃への耐性を高めよう。
tk3fftk
5
760
Dataformのリポジトリを立ち上げるときにまずやること / dataform-day0-2026
snhryt
0
160
例外の正しい扱い方 そのエラー try-catchして大丈夫?
jinwatanabe
0
240
依存関係から依存物へ―Dependencyという言葉の歴史をひも解く
j_lee
0
120
Vue × Nuxt × Oxc どこまで使える?実運用の現在地
andpad
0
250
Featured
See All Featured
Mobile First: as difficult as doing things right
swwweet
225
10k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
12
1.7k
How to Ace a Technical Interview
jacobian
281
24k
Done Done
chrislema
186
16k
Impact Scores and Hybrid Strategies: The future of link building
tamaranovitovic
0
310
Optimizing for Happiness
mojombo
378
71k
The Illustrated Guide to Node.js - THAT Conference 2024
reverentgeek
1
380
Abbi's Birthday
coloredviolet
2
8.1k
The innovator’s Mindset - Leading Through an Era of Exponential Change - McGill University 2025
jdejongh
PRO
1
200
What the history of the web can teach us about the future of AI
inesmontani
PRO
1
610
Principles of Awesome APIs and How to Build Them.
keavy
128
18k
ラッコキーワード サービス紹介資料
rakko
1
3.6M
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