@ViewChild in Angular | Data Binding | Angular 12+

  Рет қаралды 48,126

procademy

procademy

Күн бұрын

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.

Пікірлер: 45
@Naldjei3396
@Naldjei3396 7 ай бұрын
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!
@QuangVu-c9v
@QuangVu-c9v Жыл бұрын
Thank you, Sir! You're one of the best teacher on KZbin!
@nuadoixaxunuadoixaxu2182
@nuadoixaxunuadoixaxu2182 2 жыл бұрын
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 Жыл бұрын
@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 Жыл бұрын
@@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?
@ameysaitavdekar4102
@ameysaitavdekar4102 Ай бұрын
ViewChild gives fullcontrol of DOM properties over a element , Component or directive while input and output are property decorators for a particular variable in component.Also @ViewChild does not triggers any Angular hooks while input and outpt triggers ngOnChange hook.
@virajjoshi7569
@virajjoshi7569 2 жыл бұрын
@ViewChild static property can you also explain that? Thank You.
@leninnani5222
@leninnani5222 2 жыл бұрын
Nice videos, thank you
@maksat-mad
@maksat-mad Жыл бұрын
Very clear explanation, thank you!
@viniciusmachadorodrigues1724
@viniciusmachadorodrigues1724 Жыл бұрын
instead of using viewchild static, we could use dependency injection right?
@stevecriddle
@stevecriddle Жыл бұрын
Why does sayHello() get called 4 times when the page displays?
@raviverma2431
@raviverma2431 Жыл бұрын
Thank you so much Its really very much helpful 🎉🎉
@dimon.digital
@dimon.digital Жыл бұрын
Thank you very much! 👋
@VladyslavHaidar
@VladyslavHaidar 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 2 жыл бұрын
well the method only considers years , hence you get 2022-2001 = 21 years
@sivaprasad10410
@sivaprasad10410 2 жыл бұрын
in which scenario we can use @ViewChild decorator
@yurapekach7550
@yurapekach7550 Жыл бұрын
Thank you so much!
@user-gz8ub3fl6r
@user-gz8ub3fl6r Жыл бұрын
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 Жыл бұрын
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
@MultiWarrr
@MultiWarrr Жыл бұрын
How it worked without declaring var inside afterviewinit method?
@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()}}
@datarsairaghav2053
@datarsairaghav2053 Жыл бұрын
Property 'dateOfBirth' has no initializer and is not definitely assigned in the constructor. 11 @ViewChild('dobInput') dateOfBirth: ElementRef; 28th video @ViewChild video
@swathykrishna9122
@swathykrishna9122 Жыл бұрын
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.
@ciciiig
@ciciiig Жыл бұрын
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 Жыл бұрын
You could be having difficulty compiling. Try - @ViewChild(DemoComponent, {static :true}) demoComp : DemoComponent = {} as DemoComponent;
@ravishsingh1112
@ravishsingh1112 Жыл бұрын
please share link of java script course #procademy
@ahamed7794
@ahamed7794 2 жыл бұрын
Got error object is possibly undefined
@datarsairaghav2053
@datarsairaghav2053 Жыл бұрын
Property 'age' has no initializer and is not definitely assigned in the constructor. 12 @ViewChild('ageInput') age: ElementRef; ~~~
@BrisLS1
@BrisLS1 Жыл бұрын
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 2 жыл бұрын
share github code
@techsharp257
@techsharp257 2 жыл бұрын
@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 2 жыл бұрын
change @ViewChild('dobInput') dateOfBirth: ElementRef to @ViewChild('dobInput') dateOfBirth!: ElementRef @ViewChild('ageInput') age: ElementRef; to @ViewChild('ageInput') age!: ElementRef;
@deepesttrouble1
@deepesttrouble1 2 жыл бұрын
gotta add `!` like: dateOfBirth!
@ahamed7794
@ahamed7794 2 жыл бұрын
Got error : object is possibly undefined
@morishwilson7360
@morishwilson7360 Жыл бұрын
@ViewChild('dobInput') dateOfBirth!: ElementRef; Try "!" after "dateOfBirth" and before ":"
@vidhyaviswanath5441
@vidhyaviswanath5441 Жыл бұрын
Same for me😬
@daniel49245
@daniel49245 Жыл бұрын
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); }
@darshuyazh
@darshuyazh Жыл бұрын
thanks this worked for me
Friends make memories together part 2  | Trà Đặng #short #bestfriend #bff #tiktok
00:18
НАШЛА ДЕНЬГИ🙀@VERONIKAborsch
00:38
МишАня
Рет қаралды 2,6 МЛН
黑的奸计得逞 #古风
00:24
Black and white double fury
Рет қаралды 16 МЛН
小路飞嫁祸姐姐搞破坏 #路飞#海贼王
00:45
路飞与唐舞桐
Рет қаралды 29 МЛН
Angular Lifecycle Hook in Action | Lifecycle Hook | Angular 12+
29:33
@ViewChild/@ViewChildren in Angular (Tutorial 35)
19:31
Nisha Singla
Рет қаралды 65 М.
ng-content in Angular | Directives | Angular 12+
12:54
procademy
Рет қаралды 38 М.
Implementing Search Functionality | Data Binding | Angular 12+
13:44
Change Detection in Angular - Pt.1 View Checking
18:06
Decoded Frontend
Рет қаралды 38 М.
Angular Life Cycle Hooks | Lifecycle Hooks | Angular 12+
18:47
Content Projection in Angular - Complete Guide (Beginners/Advanced)
26:18
Learn flexbox the easy way
34:04
Kevin Powell
Рет қаралды 715 М.
Friends make memories together part 2  | Trà Đặng #short #bestfriend #bff #tiktok
00:18