Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular2:getting event.target.value

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??

like image 310
TikChiku Avatar asked Nov 07 '17 08:11

TikChiku


People also ask

What is event target value Angular?

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.

What is event target value in Javascript?

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.

What does a event binding use {{ }}?

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.

What is the type of $event Angular?

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.


3 Answers

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 ;)

like image 91
andrea06590 Avatar answered Nov 15 '22 00:11

andrea06590


(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

like image 43
Günter Zöchbauer Avatar answered Nov 15 '22 00:11

Günter Zöchbauer


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);
    }
   }
like image 32
Fahad Nisar Avatar answered Nov 15 '22 02:11

Fahad Nisar