how to generate the next item id when creating a new post?

Discussão em 'Angular' iniciado por Igor Shvets, Janeiro 3, 2025.

  1. Igor Shvets

    Igor Shvets Guest

    How to generate the next ID in the post, if in the add method, I add only "title" and "url"?

    Method:

    addPost(title:string, url:string):void {
    const post = {
    title:title,
    url: url
    };
    this.collection.push(post);
    }


    Template where i show post:

    <img [src]="pic.url" alt="test" class="img-responsive">
    <p class="lead"><span>{{pic.id}}:</span>{{pic.title}}</p>
    <div class="del">
    <button class="btn btn-outline-danger (click)="removePost(pic.id)">Delete</button></div>


    Interface of input data:

    export interface Picture {
    id?: number,
    title: string,
    url: string
    }


    Last post in my array:

    {
    id: 10,
    title: "beatae et provident et ut vel",
    url: "https://placekitten.com/200/200",
    }

    Continue reading...

Compartilhe esta Página