因此,我对Ionic-angular开发非常陌生,现在我只是想在home.ts文件中定义一个函数,并在home.html文件中调用它,以便在FirebaseDB中进行基本的创建。这是我的home.html文件:
home.html
<ion-header>
<ion-navbar>
<ion-title>
Ionic Blank
</ion-title>
<ion-buttons end>
<button ion-button icon-only (click)="addCourse()">
<ion-icon name="add"></ion-icon>
</button>
</ion-buttons>
</ion-navbar>
</ion-header>
<ion-content padding>
</ion-content>
这是我的home.ts文件
home.ts
import { Component } from '@angular/core';
import { NavController, AlertController } from 'ionic-angular';
import { AngularFireDatabase, AngularFireList } from 'angularfire2/database';
import { Observable } from '../../../node_modules/rxjs';
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
songsList: AngularFireList<any>;
songs: Observable<any>;
constructor(public navCtrl: NavController, public afDB: AngularFireDatabase, public alertCtrl: AlertController) {
this.songsList = this.afDB.list('/songs');
this.songs = this.songsList.valueChanges();
function addCourse(){
let prompt = this.alertCtrl.create({
title: 'Course Name',
message: "Enter the Course Code",
inputs: [
{
name: 'title',
placeholder: 'Title'
},
],
buttons: [
{
text: 'Cancel',
handler: data => {
console.log('Cancelled');
}
},
{
text: 'Save',
handler: data => {
const newSongRef = this.songs.push({});
newSongRef.set({
id: newSongRef.key,
title: data.title
});
}
}
]
});
prompt.present();
}
}
}
从function addCourse(){...}
删除声明'function'时出现错误,但在网站上收到此错误
_co.addCourse() is not a function. (In '_co.addCourse()','_co.addCourse() is undefined)
首先,我认为这是因为我应该在构造函数之外定义函数,但是随后如何调用数据库或警报控制器?
感谢帮助!
答案 0 :(得分:0)
您不应该具有关键字function,它应该在构造函数之外,将其更改为
constructor(public navCtrl: NavController, public afDB: AngularFireDatabase, public alertCtrl: AlertController) {
}
addCourse(){
}