Signal queries: Angular viewChild() and contentChild() functions

Поделиться
HTML-код
  • Опубликовано: 2 окт 2024

Комментарии • 4

  • @romanfedun577
    @romanfedun577 2 месяца назад

    thanks for the video, very informative and useful

  • @aram5642
    @aram5642 5 месяцев назад

    What is the advantage of moving the .focus() invocation from ngOnInit to constructor + effect?

    • @briantreese
      @briantreese  5 месяцев назад

      Great question! The advantage is that the life cycle hooks are not really needed in this scenario now that the viewChild() and contentChild() queries are signals. We really just want to call focus() on the element as soon as it notifies us that it's ready which is exactly what the effect will do.
      Also, if we were to switch and make the element optional, we wouldn't need to change anything with the effect. Take a look at this example (stackblitz.com/edit/stackblitz-starters-5xer3s?file=src%2Fsearch%2Fsearch.component.ts,src%2Fsearch%2Fsearch.component.html). I switched it around so that there is a two second delay before the input element is inserted. But, once it's inserted, it still receives focus and I didn't need to switch anything as far as the effect goes.
      I hope that helps!

    • @aram5642
      @aram5642 5 месяцев назад

      @@briantreese altight makes sense. Effects are always run on construction, to sort of get bootstrapped and subscribe to the signals they depend on. The bit that I missed was that they run when they are ready. Thanks for the explanation!