【发布时间】:2017-05-17 12:14:29
【问题描述】:
我有一个处理单一类型对象的应用程序。我使用服务管理对象的状态。在一个组件中,我显示对象。从这里您可以单击字段以在另一个组件中对其进行编辑。您还可以单击按钮将新项目添加到字段并删除项目。
大多数情况下它工作正常,但有时它会在没有任何错误消息的情况下中断。我单击删除或编辑按钮或字段,但没有任何反应。单击处理程序已损坏(我已在控制台中检查过)。这通常发生在我从数组中添加或删除之后。
这很令人困惑,因为当我添加一个元素时,我使用 http 来确保它被保存到数据库中,然后只有在返回带有新液滴的 observable 之后,我才让角度更新显示。并且显示总是更新。然而,有时显示更新,但占卜没有意识到,这很奇怪。所以我会添加一个提示,例如提示会在数据库中可见,被返回并更新视图,但在组件中是看不到的。
这也是不一致的。有时工作得很好。
这是视图中的一些示例代码。
<h4>Hints (optional)</h4>
<button class="btn btn-sm" [routerLink]="['/create/create5']">Add New</button>
<div *ngIf="droplet.hints.length < 1">None</div>
<div class="row" *ngFor="let hint of droplet.hints; let i=index">
<div class="hint col-md-10" (click)="selectHint(i)">
<span [innerHTML]="hint.content || empty"></span>
<span (click)="removeElement(i, 'hint')" class="pull-right glyphicon glyphicon-remove" aria-hidden="true"></span>
</div>
</div>
<h4>Tags
<div class="progress-marker" [class.complete]="droplet.tags.length > 0"></div>
<div class="progress-marker" [class.complete]="droplet.tags.length > 1"></div>
<div class="progress-marker" [class.complete]="droplet.tags.length > 2"></div>
</h4>
<button class="btn btn-sm" [routerLink]="['/create/create6']">Add New</button>
<div *ngIf="droplet.tags.length < 1">None</div>
<br>
<button *ngFor="let tag of droplet.tags; let i=index" type="button" class="btn btn-default btn-sm" (click)="removeElement(i, 'tag')">
<span class="glyphicon glyphicon-remove" aria-hidden="true"></span> {{ tag.tag }}
</button>
有几个这样的字段,但通常带有 *ngIf 的字段是导致问题的字段。正如我所说,如果我在数组中添加、删除或编辑一个元素,它会起作用并且模板会更新,但在此之后数组中通常没有任何效果(尽管非数组可以正常工作)。
相关组件代码如下所示:
import { Component, OnInit } from '@angular/core';
import { Droplet } from '../droplet';
import { DropletService } from '../droplet.service';
import { Router } from '@angular/router';
import { Subscription } from 'rxjs/Rx';
import { HttpService } from '../http.service';
export class ShowDropletComponent implements OnInit {
droplet: Droplet;
constructor(
private dropletService: DropletService,
private router: Router,
private httpService: HttpService
) { }
ngOnInit() {
this.droplet = this.dropletService.getCurrentDroplet();
this.dropletService.pushedDroplet.subscribe(
droplet => this.droplet = droplet
)
}
//using dummy to ensure element not updated unless returned from server
removeElement(index, element) {
console.log("remove clicked");
let dummy = this.droplet;
if (element === "explanation") {
this.router.navigate(['create/create3']);
dummy.explanations.splice(index, 1);
} else if (element === "question") {
this.router.navigate(['create/create4']);
dummy.questions.splice(index, 1);
} else if (element === "hint") {
this.router.navigate(['create/create5']);
dummy.hints.splice(index, 1);
} else if (element === "tag") {
dummy.tags.splice(index, 1);
}
this.httpService.saveDroplet(dummy)
.subscribe(
(droplet: Droplet) => {
this.dropletService.updateCurrentDroplet(droplet);
}
);
}
editThis(field) {
if (field === "description") {
this.router.navigate(['create/create2']);
} else if (field === "name") {
this.router.navigate(['create/create1']);
}
}
selectExplanation(index) {
console.log("select exp clicked");
this.router.navigate(['create/create3', index]);
}
selectQuestion(index) {
console.log("rselect q clicked");
this.router.navigate(['create/create4', index]);
}
selectHint(index) {
console.log("select hint clicked");
this.router.navigate(['create/create5', index]);
}
}
我的猜测是,这与 *ngFor 更新视图中的数组有关,但索引未正确更新或单击处理程序正在中断,但这不仅仅是模板特定部分中的那些。我很茫然。
【问题讨论】: