【发布时间】:2017-06-30 07:15:27
【问题描述】:
我正在构建的网络应用程序非常简单,并且使用 Angular 4 和 firebase2。它列出了一个包含歌曲的表格(标题、艺术家、
我在 Firebase 上创建了一个包含歌曲列表的对象/数组,每个对象/数组都是具有上述 3 个属性的对象。我正在努力做到这一点,当用户点击歌曲的核心时,喜欢的数量会增加一个,但我尝试过的功能似乎都不起作用。下面是我对这个 addLike 函数的尝试,以及产生的错误,下面是我的 html 模板、组件和数据结构的完整代码。任何帮助,将不胜感激。谢谢!
addLike(index){
this.songs.update(index, { likes: this.songs[index] + 1 });
}
//(23,1): Supplied parameters do not match any signature of call target.
addLike(index){
this.songs[index].update({likes: this.songs[index] + 1 });
}
//ERROR TypeError: Cannot read property 'update' of undefined
这是完整的代码
//COMPONENT HTML
<div> TEST </div>
<table class="table">
<thead>
<tr>
<th>Title</th>
<th>Artist</th>
<th>Likes</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let song of songs | async ; let i = index">
<td>{{ song.title }}</td>
<td>{{ song.artist }}</td>
<td>{{ song.likes }}
<i class="fa fa-heart-o" aria-hidden="true" *ngIf="song.likes < 1"></i>
<i class="fa fa-heart" aria-hidden="true" *ngIf="song.likes >= 1"></i>
<i class="fa fa-plus" aria-hidden="true" (click)="addLike(i)" ></i>
</td>
</tr>
</tbody>
</table>
//COMPONENT TS
import { Component } from '@angular/core';
import { AngularFireDatabase, FirebaseListObservable, FirebaseObjectObservable } from 'angularfire2/database';
import { AngularFireAuthModule,AngularFireAuth} from 'angularfire2/auth';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
})
export class AppComponent {
title = 'Oxcord';
songs: FirebaseObjectObservable<any>;
constructor(db: AngularFireDatabase) {
this.songs = db.object('/songs');
}
addLike(index){
this.songs[index].update({likes: this.songs[index] + 1 });
}
}
{
"songs" : [ {
"artist" : "J Cole",
"likes" : 3,
"title" : "No Role Modelz"
}, {
"artist" : "Michael Jackson",
"likes" : 8,
"title" : "Thriller"
}, {
"artist" : "Meek Mill",
"likes" : 0,
"title" : "Trash"
}, {
"artist" : "Kendrick",
"likes" : 6,
"title" : "Humble"
}, {
"artist" : "Missy",
"likes" : 4,
"title" : "Work It"
} ]
}
【问题讨论】:
标签: angular firebase firebase-realtime-database angularfire2