【发布时间】:2017-10-16 15:12:27
【问题描述】:
下拉服务调用 http delete service 从 firebase 服务器中的数据获取 id。然后将id 设置为http 服务中的属性idArr。然后 id 用于删除数据库条目。但它不工作。
我期待数据库中的 {data.id[0]} 字段被删除,但它没有被删除
没有抛出任何错误
//下拉服务
import { Injectable } from '@angular/core';
import { ICourseModel } from '../interface/course-model';
import { HttpPostService } from './http-post.service';
import { HttpGetService } from './http-get.service';
import { HttpDeleteService } from './http-delete.service';
@Injectable()
export class DropDownService {
courses: ICourseModel[] = [
{ course: 'Mobile Development' },
{ course: 'Web Development' },
{ course: 'IOS Development' },
{ course: 'Android Development' }
];
id: string[];
coursesDt: any = this.httpGet.getData();
private setDt() {
this.httpSer.storeData(this.courses).subscribe(
(response) => { console.log(response); },
(error) => { console.log(error); });
}
private getDt() {
this.httpGet.getData().subscribe(
(response) => { this.coursesDt = response; },
(error) => { console.log(error); });
}
getData() {
return this.courses;
}
setData(obj: { course: string }) {
this.courses.unshift(obj);
}
constructor(private httpSer: HttpPostService, private httpGet: HttpGetService, private dlt: HttpDeleteService) {
this.setDt();
// this.getDt();
console.log(this.coursesDt);
this.dlt.gtData().then(id => { this.dlt.idArr.push(...id); });
this.dlt.removeDt().then(() => console.log('ran'));
}
}
//http删除服务
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
@Injectable()
export class HttpDeleteService {
idArr = [];
public baseUrl = '';
constructor(private http: Http) {
console.log(this.idArr);
}
gtData() {
return this.http.get(`${this.baseUrl}/data.json`)
.toPromise()
.then(response => this.convert(response.json()));
}
convert(pasrsedResponse) {
return Object.keys(pasrsedResponse).map(
id => (id)
);
}
removeDt() {
return this.http.delete(`${this.idArr[0]}.json`).toPromise();
}
}
// 应用组件
ngOnInit() {
// form controls validation specicified in the class for the Reactive Forms
this.courseForm = this.fb.group({
username: [null, [Validators.required, Validators.pattern(/^[a-z0-9_-]{3,16}$/)]],
email: [null, [Validators.required, Validators.pattern('([a-zA-Z0-9_.-]+)@([a-zA-Z0-9_.-]+)\\.([a-zA-Z]{2,5})')]],
address: [null, [Validators.required, Validators.minLength(10), Validators.maxLength(100)]],
date: [null, [Validators.required]],
select: [null, [Validators.required]]
});
// passing the observable to the variable which then uses async pipe
this.route.data.subscribe((data: Data) => { this.coursesDp = data['course']; });
this.personDetail = this.fieldData.getPersonData();
}
}
【问题讨论】:
-
您能否详细说明您的代码“不起作用”的原因?你期待什么,实际发生了什么?如果您遇到异常/错误,请发布它发生的行和异常/错误详细信息。请edit这些详细信息,否则我们可能无法提供帮助。
-
我希望删除数据库中的
{data.id[0]}字段
标签: angular angular2-services angular-services angular-http