@ViewChild in Angular | Data Binding | Angular 12+

Поділитися
Вставка
  • Опубліковано 1 лют 2022
  • In this lecture, you are going to learn about @ViewChild decorator in angular. The @ViewChild decorator in angular is used to access an HTML element, a component or a directive in the component class.
    Also remember that @ViewChild decorator returns the first matching element from the DOM and assign it to the property which we have decorated it with @ViewChild.
    Let's understand @ViewChild decorator with an example.

КОМЕНТАРІ • 44

  • @leninnani5222
    @leninnani5222 2 роки тому +1

    Nice videos, thank you

  • @maksat-mad
    @maksat-mad Рік тому

    Very clear explanation, thank you!

  • @user-th5ec6zh3i
    @user-th5ec6zh3i 8 місяців тому

    Thank you, Sir! You're one of the best teacher on UA-cam!

  • @Naldjei3396
    @Naldjei3396 4 місяці тому

    Awesome how I watch your videos to learn one thing and at the end I learnt a lot more. You're a great teacher. Thank you!

  • @raviverma2431
    @raviverma2431 Рік тому

    Thank you so much
    Its really very much helpful 🎉🎉

  • @virajjoshi7569
    @virajjoshi7569 2 роки тому +7

    @ViewChild static property can you also explain that?
    Thank You.

  • @yurapekach7550
    @yurapekach7550 Рік тому

    Thank you so much!

  • @dimon.digital
    @dimon.digital Рік тому

    Thank you very much! 👋

  • @nuadoixaxunuadoixaxu2182
    @nuadoixaxunuadoixaxu2182 2 роки тому +10

    Will you please explain the difference about the use of viewchild vs @input@output... what is the advantages and disadvances over the other.

    • @sourandbitter3062
      @sourandbitter3062 Рік тому +2

      @ViewChild is to access child elements/components from the parent component.
      @Input or @Output are to access data bindings in the child component that were set inside the parent component's template.

    • @DD-vc7fq
      @DD-vc7fq Рік тому +2

      @@sourandbitter3062 That answer does not seem satisfactory to be honest.
      I guess the main question is:
      Why would you ever use @Output and EventEmitter when passing data from child component to parent component, when @ViewChild seems way more practical and does the same thing?

  • @datarsairaghav2053
    @datarsairaghav2053 9 місяців тому +2

    Property 'dateOfBirth' has no initializer and is not definitely assigned in the constructor.
    11 @ViewChild('dobInput') dateOfBirth: ElementRef;
    28th video @ViewChild video

  • @viniciusmachadorodrigues1724

    instead of using viewchild static, we could use dependency injection right?

  • @sivaprasad10410
    @sivaprasad10410 Рік тому +2

    in which scenario we can use @ViewChild decorator

  • @stevecriddle
    @stevecriddle Рік тому +2

    Why does sayHello() get called 4 times when the page displays?

  • @MultiWarrr
    @MultiWarrr Рік тому

    How it worked without declaring var inside afterviewinit method?

  • @user-gz8ub3fl6r
    @user-gz8ub3fl6r Рік тому +1

    I am getting this error when trying to access element.
    Ts:
    @ViewChild('', { static: false }) : ElementRef;
    But i am getting this error:
    Property '' has no initializer and is not definitely assigned in the constructor.
    Html:
    .
    Can you please help me on this?

    • @sourandbitter3062
      @sourandbitter3062 Рік тому +1

      You probably need to add an exclamation mark after the property name . This is called the non-null assertion operator and tells TypeScript that the property will be set at runtime even though TypeScript cannot detect it at compile time:
      @ViewChild('', { static: false })
      !: ElementRef; //

  • @siddharthakundarapu9487
    @siddharthakundarapu9487 Рік тому

    hi i have got error in ts file property @ViewChild(DemoComponent, {static: true}) demoComp: DemoComponent; ->demoComp

  • @user-hd8dm5ur3i
    @user-hd8dm5ur3i 2 роки тому +2

    Maybe, I don't get something, but it looks like if person was born on 27-th of October, 2001, he is not 21 y.o. yet - how it is explained with this method 'calculateAge()'
    Just saying
    Btw - content is very g00d - THANKS!

    • @ashishkumarnath1259
      @ashishkumarnath1259 Рік тому

      well the method only considers years , hence you get 2022-2001 = 21 years

  • @swathykrishna9122
    @swathykrishna9122 Рік тому +1

    Why statement in sayHello(), logged twice in console?

    • @shinocs7901
      @shinocs7901 Рік тому

      did you get answer for this?

    • @viniciusmachadorodrigues1724
      @viniciusmachadorodrigues1724 Рік тому

      @@shinocs7901 at least in react, it happens because its on development mode. In angular it may the the same reason.

  • @ravishsingh1112
    @ravishsingh1112 Рік тому

    please share link of java script course
    #procademy

  • @gtsin
    @gtsin Рік тому +1

    The second method you described in the video: "Accessing child component in the parent component using ViewChild decorator" is not working anymore.

    • @techie2903
      @techie2903 Рік тому

      It works.

    • @BrisLS1
      @BrisLS1 8 місяців тому

      You could be having difficulty compiling. Try - @ViewChild(DemoComponent, {static :true}) demoComp : DemoComponent = {} as DemoComponent;

  • @ahamed7794
    @ahamed7794 Рік тому

    Got error object is possibly undefined

  • @datarsairaghav2053
    @datarsairaghav2053 9 місяців тому +1

    Property 'age' has no initializer and is not definitely assigned in the constructor.
    12 @ViewChild('ageInput') age: ElementRef;
    ~~~

    • @BrisLS1
      @BrisLS1 8 місяців тому

      You can try - = {} as ElementRef; to get it to compile. I don't know why he does not have to initialize things. This may be a setting somewhere.

  • @jayakumar2927
    @jayakumar2927 Рік тому

    share github code

  • @SrinathJog
    @SrinathJog Рік тому

    here in your example if we delete even console giving error. It means not able to call function . Can you explain why it is like that ? what is the relation between and {{democomp.sayHello()}}

  • @daniel49245
    @daniel49245 Рік тому +4

    if you are getting error try this:
    @ViewChild("dobInput") dateOfBirth!: ElementRef;
    @ViewChild("ageInput") age!: ElementRef;
    and if you want more precise calculation of age:
    calculateAge(){
    let birthDate = new Date(this.dateOfBirth.nativeElement.value);
    let currentDate = new Date();
    let diffInMilliseconds = Math.abs(currentDate.getTime() - birthDate.getTime());
    let diffInYears = diffInMilliseconds / (1000 * 60 * 60 * 24 * 365.25);
    this.age.nativeElement.value = Math.floor(diffInYears);
    }

  • @techsharp257
    @techsharp257 Рік тому +1

    @ViewChild('dobInput') dateOfBirth: ElementRef;
    @ViewChild('ageInput') age: ElementRef;
    Error
    Property 'dateOfBirth' has no initializer and is not definitely assigned in the constructor.
    Property 'age' has no initializer and is not definitely assigned in the constructor.
    I am using Angular 14.

    • @benageandy8374
      @benageandy8374 Рік тому

      change @ViewChild('dobInput') dateOfBirth: ElementRef to @ViewChild('dobInput') dateOfBirth!: ElementRef
      @ViewChild('ageInput') age: ElementRef; to @ViewChild('ageInput') age!: ElementRef;

    • @deepesttrouble1
      @deepesttrouble1 Рік тому +1

      gotta add `!` like: dateOfBirth!

    • @ahamed7794
      @ahamed7794 Рік тому

      Got error : object is possibly undefined

    • @morishwilson7360
      @morishwilson7360 Рік тому

      @ViewChild('dobInput') dateOfBirth!: ElementRef;
      Try "!" after "dateOfBirth" and before ":"

    • @vidhyaviswanath5441
      @vidhyaviswanath5441 Рік тому

      Same for me😬