Angular 4 - get input value

后端 未结 9 1556
抹茶落季
抹茶落季 2021-02-01 00:41

I would like to know how to get the value from an input on angular 4. I looked over the documentation on angular and the example with the key event doesn\'t work very well for m

相关标签:
9条回答
  • 2021-02-01 01:37

    I think you were planning to use Angular template reference variable based on your html template.

     // in html
     <input #nameInput type="text" class="form-control" placeholder=''/>
    
     // in add-player.ts file
     import { OnInit, ViewChild, ElementRef } from '@angular/core';
    
     export class AddPlayerComponent implements OnInit {
       @ViewChild('nameInput') nameInput: ElementRef;
    
       constructor() { }
    
       ngOnInit() { }
    
       addPlayer() {
         // you can access the input value via the following syntax.
         console.log('player name: ', this.nameInput.nativeElement.value);
       }
     }
    
    0 讨论(0)
  • 2021-02-01 01:39

    If you dont want to use two way data binding. You can do this.

    In HTML

    <form (ngSubmit)="onSubmit($event)">
       <input name="player" value="Name">
    </form>
    

    In component

    onSubmit(event: any) {
       return event.target.player.value;
    }
    
    0 讨论(0)
  • 2021-02-01 01:40

    html

    <input type="hidden" #fondovalor value="valores">
         <button (click)="getFotoFondo()">Obtener</button>
    

    ts

    @ViewChild('fondovalor') fondovalor:ElementRef;
    
    getFotoFondo(){ 
            const valueInput = this.fondovalor.nativeElement.value
    }
    
    0 讨论(0)
提交回复
热议问题