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

แชร์
ฝัง
  • เผยแพร่เมื่อ 18 ธ.ค. 2024

ความคิดเห็น • 4

  • @romanfedun577
    @romanfedun577 5 หลายเดือนก่อน

    thanks for the video, very informative and useful

  • @aram5642
    @aram5642 8 หลายเดือนก่อน

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

    • @briantreese
      @briantreese  8 หลายเดือนก่อน

      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 8 หลายเดือนก่อน

      @@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!