我有文章列表和文章详细信息组件。
文章列表: - 文章清单。加载到html作为表。处理了一个click事件并设置了文章ID - 使用article id属性向文章详细信息发送信号,以便作为@Input属性加载。
文章详情: - @Input属性从列表中侦听并注册ng-OnChange事件来处理任何更改 - 加载文章详细信息视图模型(更详细) - 使用Reactive Form将所有属性加载到正确的formgroup。 - 使用样本属性并在UI中显示Reactive表单属性,只是为了知道@Input属性从列表中获取正确的值。
现在,我在Sample属性中获得了正确的值。并且还从Article Detail change事件中调用服务方法。它还设置了视图模型。但问题在这里,查看显示一键旧数据。
如果我点击第二篇文章,我的示例属性会正确更新,这证明了我的详细信息组件获取信息,最终视图模型也会在服务响应后得到更新。但是View显示旧的View Model。
每次点击最后存储的viewmodel数据都会显示在View上。任何想法为什么? 这是代码:
**#Article-List Component.ts**
@Component({
selector: 'admin-article-list',
templateUrl: './article-list.component.html',
styleUrls: ['./article-list.component.css']
})
export class ArticleListComponent extends BaseImplementationComponent {
public selectedArticleId: string;
articleClick(event: Event, articleViewModel: ArticleViewModel) {
this.selectedArticleId = articleViewModel.articleId;
}
}
**#Article-List-Html**
<div *ngFor="let article of articles">
<div class="row article" (click)="articleClick($event, article)">
<div class="col-xs-3 col-sm-3">
<a href='{{article.articleId}}'>{{article.articleId}}</a>
</div>
</div>
<div>
<admin-article-detail *ngIf="isReadyToLoad" [articleId]="selectedArticleId"></admin-article-detail>
</div>
</div>
**#Article-Detail-Component.ts**
@Component({
selector: 'admin-article-detail',
templateUrl: './article-detail.component.html',
styleUrls: ['./article-detail.component.css']
})
export class ArticleDetailComponent extends BaseImplementationComponent {
@Input()
articleId: string;
articleViewModel: ArticleViewModel;
articleDetailFormGroup: FormGroup;
constructor(private formBuilder: FormBuilder, private articleService: ArticleService) {super();
this.createArticleDetailForm();
}
protected ngOnChangesAtBaseComponent(): void {
this.loadArticleDetail();
this.patchArticleDetailForm();
this.articleIdComingfromParent = this.articleId;
}
private loadArticleDetail() {
this.articleService.getArticle(this.articleId)
.map(item => this.articleViewModel = item)
.subscribe(
(item) => {
console.log(item);
this.articleViewModel = item;
},
(err) => {
console.log(err);
});
}
private patchArticleDetailForm() {
//console.log(this.articleViewModel.articleTitle);
this.articleDetailFormGroup.get('articleBasicDetail').patchValue({
articleTitle: this.articleViewModel.articleTitle });
}
private createArticleDetailForm() {
this.articleDetailFormGroup = this.formBuilder.group({
articleBasicDetail: this.formBuilder.group({
articleTitle: ''
})
});
}
}
**#Article-Detail-Html**
<form class="form-horizontal" [formGroup]="articleDetailFormGroup">
<div class="form-group" formGroupName="articleBasicDetail">
<div class="col-sm-12">
<label for="articleTitle" class="center-block">Street:
<input type="text" id="articleTitle" class="form-control" formControlName="articleTitle">
Updated from: {{articleIdComingfromParent}}
</label>
</div>
</div>
</form>
答案 0 :(得分:1)
我认为如果输入的引用发生更改,子组件将触发更改,您有这个错误,因为您正在直接更改输入,这里有一个解决方案,使用OnPush检测策略和doCheck
@Component({
selector: 'admin-article-detail',
templateUrl: './article-detail.component.html',
styleUrls: ['./article-detail.component.css'],
changeDetection: ChangeDetectionStrategy.OnPush
})
@Input() articleId: string;
selectedArticle : string; // create this new property and use it
constructor(private cd: ChangeDetectorRef) {}
ngOnInit() { this.selectedArticle = this.articleId; }
ngDoCheck() {
if (this.selectedArticle !== this.articleId) {
this.selectedArticle = this.articleId;
this.cd.markForCheck();
}
}