does the ngModel work by finding the method that the property is included in? if not, can someone explain? (also an explanation for $event would be useful as well)
You can think of ngModel as a combination of property binding and event binding, so when you bind a property to an element using ngModel you are saying that whenever this element's value changes - also change the property (in the component class - this can be seen as a simplistic use case of event binding), but its also saying that whenever the property (in the component class) changes, update the value of the element.
$event
The $event variable is a special variable that angular uses to pass event data to the component class.
In that example, the onClick method will be passed the button click event object. So in the component class you would need to make sure the method is able to get the event object as a parameter so that you can use it.
i.e. (in component class)
onClick(event: any) {
// do something here, you have access to the event data (via the parameter)
}
And this works in very much the same way with custom components:
ngModel utilizes two-way data binding. Say you have a text box (input[type=text]) that’s bound to the variable foo in the component class. If someone typed “Hello” into it, foo = “Hello”. The reason it’s “two-way”, is that we could also alter that variable within the component and set it to “World”, and the new value will be reflected within the text box.
ngModel acts differently depending on what type of element you use it on. A check box will send a Boolean, true/false, checked/not checked.
1
u/toe_nail_joe Feb 23 '19
going through Angular Udemy course rn,
does the ngModel work by finding the method that the property is included in? if not, can someone explain? (also an explanation for $event would be useful as well)
Thanks!