如何解决我的角度渲染问题
我有这个HTML,我想在数组中渲染所有项目,并且在某些项目更改时也对服务的更新做出反应(像“名称”一样更新)。
当我发送更新和服务返回array.slice()时,问题出现了,似乎存在一些问题,因为我可以在数组中看到其他项目,而不是我最近更新的项目。组件和服务之间的通信类似。有三个组件:
- 列表COMPONENT呈现数组。
- 进行更新和编辑并将其发送到
- 具有所有数组并进行CRUD操作的组件3 ServiceComponent。将更新的值发送到服务。
这是我的listComponent.html及其.TS代码:
<div *ngFor="let item of items; let i = index">
<h4>{{item.heading}}</h4>
<p>{{item.details}}
<a [routerLink]="[i]"><span>Link here</span></a>
</p>
</div>
文件:
ngOnInit(): void {
this.items = this.itemsService.getitems()
this.subscription = this.itemsService.itemChanged.subscribe((items : Item[]) => {
this.items = items;
})
}
Service.ts的代码段。
export class itemsService {
itemChanged = new Subject<Item[]>();
private items: Item[] = [
new Item(
'Heading-1',
'Lorem ipsum dolor sit amet consectetur adipisicing elit. Veritatis, dignissimos inventore, d.'
),
new Item(
'Heading-2',
'Lorem ipsum dolor sit amet consectetur adipisicing elit. Veritatis')
]
getItems() {
return this.items.slice();
}
}
updateItem(index: number, newItem: Item) {
this.items[index] = newItem;
this.itemChanged.next(this.items.slice());
}
这是
onSubmit() {
if (this.editMode){
console.log(this.editMode)
this.itemsService.updateItem(this.id, this.iForm.value);
}
}
.ts
I have this HTML where I want to render all my items in an array and also react to the updates from the service when some item changed (updated partially like a 'name').
The problem come in when I send the update and the service returns the array.slice(), there seems to be some problem because I would be able to see the other items in the array but not the one which I recently updated. The communication between the component and the service works like this. There are three components:
- listComponent that renders the array.
- editComponent that does the update and edit and sends it to component 3
- serviceComponent that have all the arrays and does the CRUD operations. Sends the updated value to the service.
Here is my listComponent.html and its .ts code:
<div *ngFor="let item of items; let i = index">
<h4>{{item.heading}}</h4>
<p>{{item.details}}
<a [routerLink]="[i]"><span>Link here</span></a>
</p>
</div>
The .ts file:
ngOnInit(): void {
this.items = this.itemsService.getitems()
this.subscription = this.itemsService.itemChanged.subscribe((items : Item[]) => {
this.items = items;
})
}
Here's a code snippet of the Service.ts:
export class itemsService {
itemChanged = new Subject<Item[]>();
private items: Item[] = [
new Item(
'Heading-1',
'Lorem ipsum dolor sit amet consectetur adipisicing elit. Veritatis, dignissimos inventore, d.'
),
new Item(
'Heading-2',
'Lorem ipsum dolor sit amet consectetur adipisicing elit. Veritatis')
]
getItems() {
return this.items.slice();
}
}
updateItem(index: number, newItem: Item) {
this.items[index] = newItem;
this.itemChanged.next(this.items.slice());
}
And here is the editComponent.ts:
onSubmit() {
if (this.editMode){
console.log(this.editMode)
this.itemsService.updateItem(this.id, this.iForm.value);
}
}
The id in the editComponent is received from the params.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您有两种解决问题的方法。实际上,这都是关于检测视图中的变化。
第一种方法 - 只需在组件中添加
更改eTectorRef
。这样:但是如上所述,在评论中 - 这不是最好的解决方案。最好的是使用
async
管道,您可以这样做:在组件文件中:
You have two approaches to solve your problem. Actually it's all about detect changes in a view.
First way - just add
ChangeDetectorRef
to your component. Like this:But as mark above in comments - that's not the best solution. The best is to use
async
pipe, you may do it this way:and in you component file:
问题在于某些形式的命名惯例。例如。
在这里,我认为“ ngmodel名称”将是{对象对象}的键,而是“名称”是键。
The problem lies with some naming convention in the forms. For eg.
Here, I thought the 'ngModel name' would be the key of the {object Object} but instead the 'name' is the key.