简体   繁体   English

将输入值保存在角度为5的变量中

[英]save the value of an input in a variable in angular 5

I am new at angular and I am trying to get the value of an input and save it in a variable to pass it to my service that needs a parameter: 我是angular的新手,我试图获取输入的值并将其保存在变量中,以将其传递给需要参数的服务:

this is my html 这是我的HTML

<form class="main-form">

  <div class="form-group col-md-4">
    <label for="cedula">Numero de cedula</label>
    <input type="text" class="form-control" id="cedula" name="cedula [(ngModel)]="cedula" placeholder="Ingrese su numero de cedula">
   <br>
   <button type="submit" (click)="getReservas(cedula)" class="btn btn-primary">Consultar</button>

  </div>

</form>
<h1>Usuario Reservado</h1>
<div class="clear"></div>
<table class="table" >
  <thead>
    <tr>

      <th scope="col">Cedula</th>
      <th scope="col">Nombre</th>
      <th scope="col">Edad</th>
      <th scope="col">Fecha</th>
      <th scope="col">Origen</th>
      <th scope="col">Destino</th>
      <th scope="col">Hora</th>
      <th scope="col">Telefono</th>
      <th scope="col">Precio</th>
    </tr>
  </thead>
  <tbody>
    <tr *ngFor="let item of reserva">

      <td>{{item.cedula}}</td>
      <td>{{item.nombre}}</td>
      <td>{{item.edad}}</td>
      <td>{{item.fechar}}</td>
      <td>{{item.origenr}}</td>
      <td>{{item.destinor}}</td>
      <td>{{item.hora}}</td>
      <td>{{item.telefono}}</td>
      <td>{{item.precior}}</td>

    </tr>

  </tbody>
</table>

this is my component 这是我的组成部分

import { Component, OnInit } from '@angular/core';
import {Router} from '@angular/router';

import {ReservasService} from './reservas.service';
import {ReservasModel} from './../modelo/reservas.model';

@Component({
  selector: 'app-reservas',
  templateUrl: './reservas.component.html',
  styleUrls: ['./reservas.component.css'],
  providers:[ReservasService]
})
export class ReservasComponent implements OnInit {
    private reserva:Array<ReservasModel>;
    private cedula:String;
  constructor(private rs:ReservasService) { }

  ngOnInit() {
    this.loadRes();
  }

private loadRes():void{
    this.rs.getReservas(this.cedula).subscribe(res=>{
        this.reserva=res;

    });
}
}

my service 我的服务

import { Injectable } from '@angular/core';
import  {HttpClient} from '@angular/common/http';
import { Observable } from 'rxjs';

import {ReservasModel} from './../modelo/reservas.model';


@Injectable({
  providedIn: 'root'
})
export class ReservasService {

  constructor(private http:HttpClient) { }

  public getReservas(cedula:String):Observable<ReservasModel[]>{

    return this.http.get<ReservasModel[]>("http://localhost:8080/getConsultarCc?cc="+cedula);

  }
}

what am I doing wrong, I would thank whoever can help me. 我做错了什么,我感谢任何可以帮助我的人。

try this form but I get error: https://stackoverflow.com/a/39959287/10543023 and https://es.stackoverflow.com/a/5845 试试这个表格,但我收到错误消息: https : //stackoverflow.com/a/39959287/10543023https://es.stackoverflow.com/a/5845

You are missing closing the " in the input field. You should be closing the " in order for the angular two way binding to work using ngModel . 这是因为丢失关闭"输入字段中。你应该关闭" ,以使角双向绑定使用工作ngModel

HTML HTML

<input type="text" class="form-control" id="cedula" name="cedula" [(ngModel)]="cedula" placeholder="Ingrese su numero de cedula">

TS TS

private cedula:string;

I full working example: https://stackblitz.com/edit/angular-tfjpgu 我完整的工作示例: https : //stackblitz.com/edit/angular-tfjpgu

Two more things: 还有两件事:

1) Also you should be using string instead of String . 1)同样,您应该使用string而不是String Take a look to the answers here: Typescript: difference between String and string 看一下这里的答案: Typescript:字符串和字符串之间的区别

2)if you are going to be using private variables make sure you use getters and setters. 2)如果要使用私有变量,请确保使用getter和setter方法。 I don't suggest you to make them private unless they are for constructors or classes. 我不建议您将它们设为私有,除非它们用于构造函数或类。 it's not good practice to not declare getters and setters on private variables in the components. 在组件的私有变量上不声明getter和setter方法不是一个好习惯。

Typescript Class with private variables . Typescript具有私有变量的类 If you decide to leave the private varible change your code after you have created the setters. 如果您决定保留私有变量,请在创建设置器后更改代码。

private loadRes():void{
    this.rs.getReservas(this.getCedula()).subscribe(res=>{
        this.reserva=res;

    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM