Created
August 27, 2016 10:22
-
-
Save okunokentaro/5e7e1ecc001f47536650c7870a5f3ba4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Component } from '@angular/core'; | |
class Hero { | |
id: number; | |
name: string; | |
} | |
@Component({ | |
selector: 'my-app', | |
template: ` | |
<h1>{{title}}</h1> | |
<label>曲名: <input type="text" [(ngModel)]="songTitle"></label> | |
<label>歌手名: <input type="text" [(ngModel)]="artistName"></label> | |
<button (click)="send(songTitle, artistName)">追加</button> | |
<ul> | |
<li *ngFor="let song of songList">{{song.title}} ({{song.artist}})</li> | |
</ul> | |
` | |
}) | |
export class AppComponent { | |
public title = '失恋ソング'; | |
result: string; | |
songListRef = (<any>window).firebaseApp.database().ref('songList'); | |
songList: {title: string; artist: string}[]; | |
ngOnInit() { | |
this.songListRef.on('value', (dataSnapshot) => { | |
const rawObject = dataSnapshot.val(); | |
this.songList = Object.keys(rawObject).map((key) => { | |
return rawObject[key]; | |
}); | |
}); | |
} | |
send(songTitle: string, artistName: string) { | |
if (!songTitle || !artistName) { | |
alert('曲名とアーティスト名を入力してください。'); | |
return; | |
} | |
const newSongRef = this.songListRef.push(); | |
newSongRef.set({ | |
title: songTitle, | |
artist: artistName | |
}); | |
this.songTitle = ''; | |
this.artistName = ''; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment