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
77
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
160
EKS Auto-Mode with Kro
y0hgi
0
250
AWS Cloud Control API & AWSCC Provider
y0hgi
1
90
AWSとSRE 〜サービスの信頼性〜
y0hgi
2
390
re:Invent 2024 re:Cap コンピューティング&コンテナ
y0hgi
3
500
クラウドを今から学ぶには
y0hgi
0
760
クラウド・コンテナ・CI/CDわからん会
y0hgi
0
90
入門 Docker - JAWS-UG東京 ランチタイムLT会 #14
y0hgi
1
470
AWS CloudShell で開発したかった話 / i-cant-develop-in-cloudshell
y0hgi
1
2.1k
Other Decks in Programming
See All in Programming
承認済みなのに差戻しできてしまうバグ、型で潰せます
shinchit
0
120
Claude Code全社展開のためにやったことn選~プラグイン302個・コミッター271人を支えるために~
kenchan
5
1.8k
Detecting Compromised CI with eBPF and Cilium Tetragon
lizrice
0
270
AWS Transform Customによる Spring Boot 2.xから4.xへのVerUp
satoshi256kbyte
2
110
書籍「プロフェッショナルAI駆動開発」紹介スライド
juntaromatsumoto
0
790
AI駆動開発にグラフDBを重ねてみた
satoshi256kbyte
1
370
Go 1.27からのGODEBUG / Go 1.27 リリースパーティ #go127party
mazrean
0
260
「つくるAI」だけではバグは見つからない ~テストに必要な「見つけるAI」を分離させる戦略~
mfunaki
0
200
高専キャリア LT 発表内容
crysta1221
4
4.3k
S3 を使うアプリケーションをローカル完結で動かすことに全力を注いでみた / Running S3 Apps Offline
contour_gara
0
670
30年振りにコンパイラの定数整数除算を改善した
herumi
9
4.3k
思考垂れ流し開発 ~音声入力 × AIエージェント × 開発ハーネスによる試行錯誤~
npostring
0
420
Featured
See All Featured
Between Models and Reality
mayunak
4
410
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
141
35k
The Limits of Empathy - UXLibs8
cassininazir
1
620
From Legacy to Launchpad: Building Startup-Ready Communities
dugsong
0
310
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
123
22k
Navigating the moral maze — ethical principles for Al-driven product design
skipperchong
2
500
Measuring Dark Social's Impact On Conversion and Attribution
stephenakadiri
2
260
Jess Joyce - The Pitfalls of Following Frameworks
techseoconnect
PRO
1
380
Building an army of robots
kneath
306
46k
The State of eCommerce SEO: How to Win in Today's Products SERPs - #SEOweek
aleyda
2
11k
How to Talk to Developers About Accessibility
jct
2
530
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
16
2.1k
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