如何解决我的角度渲染问题

发布于 2025-02-10 12:15:34 字数 1833 浏览 4 评论 0原文

我有这个HTML,我想在数组中渲染所有项目,并且在某些项目更改时也对服务的更新做出反应(像“名称”一样更新)。

当我发送更新和服务返回array.slice()时,问题出现了,似乎存在一些问题,因为我可以在数组中看到其他项目,而不是我最近更新的项目。组件和服务之间的通信类似。有三个组件:

  1. 列表COMPONENT呈现数组。
  2. 进行更新和编辑并将其发送到
  3. 具有所有数组并进行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:

  1. listComponent that renders the array.
  2. editComponent that does the update and edit and sends it to component 3
  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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

べ繥欢鉨o。 2025-02-17 12:15:34

您有两种解决问题的方法。实际上,这都是关于检测视图中的变化。

第一种方法 - 只需在组件中添加更改eTectorRef。这样:

constructor(private cdRef: ChangeDetectorRef) {} 

ngOnInit(): void {
    this.items = this.itemsService.getitems()
    this.subscription = this.itemsService.itemChanged.subscribe((items : Item[]) => {
      this.items = items;
      this.cdRef.detectChanges();
    })
  }

但是如上所述,在评论中 - 这不是最好的解决方案。最好的是使用async管道,您可以这样做:

<div *ngFor="let item of items$ | async; let i = index">
  ...

在组件文件中:

ngOnInit(): void {
    this.items$ = this.itemsService.itemChanged.pipe(
      startWith(this.itemsService.getitems()),
    );
  }

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:

constructor(private cdRef: ChangeDetectorRef) {} 

ngOnInit(): void {
    this.items = this.itemsService.getitems()
    this.subscription = this.itemsService.itemChanged.subscribe((items : Item[]) => {
      this.items = items;
      this.cdRef.detectChanges();
    })
  }

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:

<div *ngFor="let item of items$ | async; let i = index">
  ...

and in you component file:

ngOnInit(): void {
    this.items$ = this.itemsService.itemChanged.pipe(
      startWith(this.itemsService.getitems()),
    );
  }
隱形的亼 2025-02-17 12:15:34

问题在于某些形式的命名惯例。例如。

<input name = 'name' ngModel name='itemName'>

在这里,我认为“ ngmodel名称”将是{对象对象}的键,而是“名称”是键。

The problem lies with some naming convention in the forms. For eg.

<input name = 'name' ngModel name='itemName'>

Here, I thought the 'ngModel name' would be the key of the {object Object} but instead the 'name' is the key.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文