8

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

5 Answers 5

5
(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

Sign up to request clarification or add additional context in comments.

10 Comments

What would it not work? And what does "not work" mean exactly?
right, console.log("val:" + val) in class Detail anwsered val:. nothing is in it.
I updated my answer. See also stackblitz.com/edit/angular-vix2nq
Thank you so much for the advises and even example,but somehow (input) doesn't let console show the value. I think its related to mdAutocomplete.
Sorry, I don't understand what you mean with "doesn't let console show the value". It's printed to the console in my stackblitz
|
4

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

Comments

1

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

Comments

1

What I did and worked for me (with angular 15) is like bellow:

In HTML File

<input matInput type="text" (keyup)="doFilter($event)" placeholder="filter">

Inside the component:

doFilter(filterValue?: Event) { 
      console.log("Filter: ", (filterValue?.target as HTMLInputElement).value);
      if((filterValue?.target as HTMLInputElement).value != null) {
        this.dataSource.filter = (filterValue?.target as HTMLInputElement).value.trim().toLowerCase();
      }
    }

I hope it will help someone :)

Happy coding!

Comments

0

Use a component method to implement the cast, it is not possible to cast in templates!

@Component({
  selector: '...',
  template: '<input (keyup)="filter(target($event).value)">',
})
export class SomeCmp {
  target(event: KeyboardEvent): HTMLInputElement {
    if (!(event.target instanceof HTMLInputElement)) {
      throw new Error("wrong target");
    }
    return event.target;
  }
}

Also see https://github.com/angular/angular/issues/35293

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.