0

I'm trying to delete permanently an element of this object array:

 playlists: object[] = [
        { title: 'Ejemplo 1', songs: [ 'cancion1', 'cancion2', 'cancion3'] },
        { title: 'Ejemplo 2', songs: [ 'cancion1', 'cancion2', 'cancion3' ] }
  ];

I've tried with the splice method but it when I refresh the webpage it appears again:

deletePlaylist( index: number){

      this.playlists.splice(index, 1);

  }

Here from where does index come from:

<div *ngFor="let playlist of playlists; let i = index" class="card text-white bg-success m-3 animated fadeIn" style="max-width: 18rem;">
        <div class="card-header">{{ playlist.title }}</div>
        <div class="card-body">
            <h5 class="card-title">{{ playlist.title }}</h5>
            <ul *ngFor="let song of playlist.songs" class="card-text">
                <li>{{ song }}</li>
            </ul>
            <div class="row">
                <div class="col text-right">
                    <button (click) = "deletePlaylist( i )" class="btn btn-danger text-left" ><i class="fas fa-trash"></i></button>
                </div>
            </div>
        </div>
    </div>

Any ideas?

1

2 Answers 2

1

By refreshing your webpage, you loose any current state of your application. The component is once again created with the playlists array containing 2 items. If you want to keep track of data changes when refreshing your webpage, you need to persist it.

Sign up to request clarification or add additional context in comments.

Comments

0

You can save to local storage or session storage. Example

localStorage.setItem(key, JSON.stringify(val));

and you can get with:

var myObject = JSON.parse(localStorage.getItem('key'));

see

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.