【发布时间】:2018-01-22 15:30:44
【问题描述】:
我的个人资料类有属性“lessonsID”。我在 my-offers.component 中订阅了 getProfile(),它成功返回了配置文件。问题是,当我在我的公告中单击“删除”时,my-offers.component 中的订阅配置文件不会更改,但是当我查看存储该配置文件的后端时,配置文件中的课程 ID 被删除。为什么订阅不起作用?如何解决?
我的offers.component.html
<ul class="list">
<li
class="list__item"
*ngFor="let privateLesson of privateLessons"
>
<app-my-announcement [privateLesson]="privateLesson"></app-my-announcement>
</li>
</ul>
my-offers.component.ts
export class MyOffersComponent implements OnInit {
myOffersId: string[];
privateLessons: PrivateLesson[] = [];
constructor(
private _authService: AuthService,
private _privateLessonsService: PrivateLessonsService
) { }
ngOnInit() {
this._authService.getProfile().subscribe(
profile => {
this.myOffersId = profile.user.lessonsID;
this.privateLessons = [];
this.myOffersId.filter(offerID => {
this._privateLessonsService.getPrivateLessonByID(offerID).subscribe(
privateLesson => {
this.privateLessons.push(privateLesson);
}
);
});
},
err => {
console.log(err);
return false;
}
);
}
我的公告.components.html
<div class="announcement">
<div class="announcement__data">
<div class="data__title">
{{ privateLesson.title }}
</div>
</div>
<div class="announcement__options">
<ul class="options">
<li
class="options__item"
(click)="onPreviewClick(privateLesson._id)"
>
Podgląd
</li>
<li
class="options__item"
(click)="onDeleteClick(privateLesson._id)"
>
Usuń
</li>
</ul>
</div>
</div>
我的公告.component.ts
export class MyAnnouncementComponent implements OnInit {
@Input() privateLesson: PrivateLesson;
privateLessons: PrivateLesson[];
constructor(
private router: Router,
private authService: AuthService,
private _privateLessonsService: PrivateLessonsService
) { }
onDeleteClick(id: string) {
this._privateLessonsService.deletePrivateLessonByID(id).subscribe();
this.authService.removeLesson(id);
}
}
auth.service.ts
@Injectable()
export class AuthService {
getProfile() {
let headers = new Headers();
this.loadToken();
headers.append('Authorization', this.authToken);
headers.append('Content-Type', 'application/json');
return this.http.get( 'http://localhost:3000/users/profile', { headers: headers } )
.map( res => res.json() );
}
}
【问题讨论】:
-
您的删除订阅没有任何作用。
this._privateLessonsService.deletePrivateLessonByID(id).subscribe();?还有this.authService.removeLesson(id)应该怎么做? -
我在数据库中有两个数据集合,一个包含所有课程,另一个包含用户。 This.authService.removeLesson(id) 从实际登录用户的属性课程 ID 中删除 id。 This._privateLessonsService.deletePrivateLessonByID(id) 从存储所有课程的数据库中删除课程
标签: angular