1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

new Set () with Angular signals

Discussão em 'Angular' iniciado por RaulGM, Setembro 11, 2024.

  1. RaulGM

    RaulGM Guest

    I was wondering how to use a new Set () javascript's object with Angular "new" Signals. As I've been a long time playing with it, I decided to post it in here. Just a simple adding and deleting.

    set = signal (new Set ()); // initialize as an empty set, no need the <Set> because it's implicit.

    toggleOptionFromSet (option: string) { // just a function to toggle an option in and out of a signal Set.

    if (this.set ().has (option)) { // if the set contains the value...
    this.set.update (set => { // deletes the option of the set.
    // you cannot return set.delete (option) because it returns a boolean and update needs to return a Set.
    set.delete (option)
    return set;
    });
    console.log (this.set ()); // you see that the option has been erased.

    } else { // if the set () does not contain the option...
    this.set.update (set => set.add (option)); // this actually returns a new set, so, as you might know, you don't need to return it explicitly.
    console.log (this.set ()); // the option has been added to the set.
    }
    }


    Hope it helps someone!

    Continue reading...

Compartilhe esta Página