Skip to content

Instantly share code, notes, and snippets.

@okunokentaro
Created August 27, 2016 10:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save okunokentaro/5e7e1ecc001f47536650c7870a5f3ba4 to your computer and use it in GitHub Desktop.
Save okunokentaro/5e7e1ecc001f47536650c7870a5f3ba4 to your computer and use it in GitHub Desktop.
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