O
O
Ole892019-09-27 12:18:21
Angular
Ole89, 2019-09-27 12:18:21

How to update the data of the "child" model?

Hello everybody!
There are news with tags attached to them, that is, the News model is as follows:

import {Tags} from "./Tags";
export class News{
    id: number;
    title: string;
    text: string;
    tags: Tags;
}

Accordingly, it is this json structure that comes from the backend, that is, for each news, an array of tags comes.
Interested in how to update data when adding / removing a tag (all data comes from the backend)? If we update the news, then everything is clear, in the template new ones are simply loaded immediately after the response:
component:
this.http.get<News>('http://site.ru/api/add_news?news_id=' + news_id)
            .subscribe(
              data => { this.news = data; },
              error => { });

template:
<div *ngFor="let item of news">
  <p> {{item.title}} </p>
  <p> {{item.text}} </p>
</div>

But how do I update the tags? I'm trying to do this, but it doesn't work (and not surprisingly):
component:
this.http.get<Tags>('http://site.ru/api/add_tags?tag_name=' + tag_name)
            .subscribe(
              data => { this.tags = data; },
              error => { });

template:
<div *ngFor="let item of news">
  <p> {{item.title}} </p>
  <p> {{item.text}} </p>
  <div name="tags" *ngFor="let tag of item.tags">
  </div>
</div>

How to deal with the display of new tags, given that this is a "child" model?
Or maybe I'm not using the most optimal way?
Thanks

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question