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
Semantic Version 単位で戦略を柔軟に変えて、パッケージアップデートを自動化する
daitasu
1
240
New "Type" system on PicoRuby
pocke
1
930
エンジニアと一緒にテストコードの設計と実装を改善した話
mototakatsu
0
180
フロントエンドとバックエンドで「1文字」を揃えよう
youkidearitai
PRO
0
690
Javaの型とAI時代に型が大事な理由 / java types and type in AI era
kishida
2
140
気づいたらRubyで100作品 ー クリエイティブコーディングが生活の一部になるまで / 100 Ruby Sketches Later: How Creative Coding Became Part of My Life
chobishiba
3
580
Oxlintのカスタムルールの現況
syumai
6
1.1k
Signal Forms: Details & Live Coding @enterJS 2026 in Mannheim
manfredsteyer
PRO
0
140
Dataformのリポジトリを立ち上げるときにまずやること / dataform-day0-2026
snhryt
0
160
決定論的オーケストレーションの設計と実装 / Design and Implementation of Deterministic Orchestration
nrslib
4
1.4k
ECSアプリログをFireLensでコスト削減しようとしたけど諦めた話 in Fargate×Node.js
akihisaikeda
2
4.2k
TSKaigi Night Talks 2026_TypeScriptでサプライチェーンの整合性を型に閉じ込める
geekplus_tech
0
350
Featured
See All Featured
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
34
2.8k
New Earth Scene 8
popppiees
3
2.3k
How People are Using Generative and Agentic AI to Supercharge Their Products, Projects, Services and Value Streams Today
helenjbeal
1
210
Kristin Tynski - Automating Marketing Tasks With AI
techseoconnect
PRO
0
270
Between Models and Reality
mayunak
4
340
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
The Organizational Zoo: Understanding Human Behavior Agility Through Metaphoric Constructive Conversations (based on the works of Arthur Shelley, Ph.D)
kimpetersen
PRO
0
360
Joys of Absence: A Defence of Solitary Play
codingconduct
1
390
Building a A Zero-Code AI SEO Workflow
portentint
PRO
0
590
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
32
3.4k
jQuery: Nuts, Bolts and Bling
dougneiner
66
8.5k
Building a Modern Day E-commerce SEO Strategy
aleyda
45
9.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