I want to get event.target.value
by the codes below.
<input
mdInput
#owner
required
placeholder="荷主"
[formControl]="detail.ownerTx"
[mdAutocomplete]="autoTxt"
(change)="detail.changeOwner(owner.value)"
>
class Detail {
changeOwner(val: string){
console.log(val);
}
}
but the answer of the console.log(val)
is nothing.... any idea to actually do the data-binding??
The $event object often contains information the method needs, such as a user's name or an image URL. The target event determines the shape of the $event object. If the target event is a native DOM element event, then $event is a DOM event object, with properties such as target and target.
The target event property returns the element that triggered the event. The target property gets the element on which the event originally occurred, opposed to the currentTarget property, which always refers to the element whose event listener triggered the event.
To bind to an event you use the Angular event binding syntax. This syntax consists of a target event name within parentheses to the left of an equal sign, and a quoted template statement to the right.
Angular includes $event that contains the information about an event. The type of $event depends on the target event, e.g., if the target event is a native DOM element event, then it is an object. A component should define the onShow(event) method where the type of the parameter can be KeyboardEvent, MouseEvent, etc.
I think you should use (keyup) or another keyboard event handler if you want to get this value using (keyup)="changeInput($event)" then you can access you DOM event and value ;)
(input)="detail.changeOwner($event.target.value)"
or
ngModel (ngModelChange)="detail.changOwner($event)"
You can also subscribe to valueChanges
on the form control (detail.ownerTx
)
See also https://angular.io/api/forms/AbstractControl#valueChanges
change the input line to this
<input
mdInput
#owner
required
placeholder="荷主"
[formControl]="detail.ownerTx"
[mdAutocomplete]="autoTxt"
(change)="detail.changeOwner($event)">
and function to this :
class Detail {
changeOwner($event){
//You will get the target with bunch of other options as well.
console.log($event.target.value);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With