我可以从Angular中填充JSON的表格,没有错误
我正在阅读查询数据库,它可以完美地执行查询,因为我可以阅读控制台,JSON还可以,但表不足。
这是我的组件
@Component({
selector: 'app-envios',
templateUrl: './envios.component.html',
styleUrls: ['./envios.component.css']
})
export class EnviosComponent {
constructor(private conexion: ConexionService) {}
envios: Envio[];
@Input() idnum: number
ngOnInit() {
}
buscarEnvio()
{
const idnum = parseInt((document.getElementById('idenvio')as HTMLInputElement).value);
console.log(idnum);
this.conexion.consultarEnvio(idnum);
}
}
,这是我的conexion服务,
export class ConexionService {
envio : Envio[];
private api = 'http://localhost:8080/ProyectoFinal/webapi';
consultarEnvio(id: Number)
{
const path = `${this.api}/estadoenvio/${id}`;
return this.http.get<Envio[]>(path).subscribe(envio => {
console.log(envio);
});;
}
}
这是HTML
<div class="container-fluid">
<div class="input-group">
<input type="text" class="form-control" placeholder="CC" id="idenvio">
<span class="input-group-btn">
<button type="submit" class="btn btn-danger" type="button" (click)="buscarEnvio()">Buscar</button>
</span>
</div>
<br>
<div class="col" id="tabla">
<table class="table table-border">
<thead>
<tr class="table-danger">
<th scope="col">Id del envío</th>
<th scope="col">Nombre del destinatario</th>
<th scope="col">Dirreción de envío</th>
<th scope="col">Código Postal</th>
<th scope="col">Intentos de entrega</th>
<th scope="col">Estado del envio</th>
</tr>
</thead>
<tbody>
<tr class="table-danger" *ngFor="let envio of envios">
<td class="table-danger">{{envio.idEnvio}}</td>
<td class="table-danger">{{envio.nombreDestinatario}}</td>
<td class="table-danger">{{envio.direccionCompleta}}</td>
<td class="table-danger">{{envio.codigoPostal}}</td>
<td class="table-danger">{{envio.numIntentosEntrega}}</td>
<td class="table-danger">{{envio.idEstadoEnvio}}</td>
</tr>
</tbody>
</table>
</div>
</div>
,如果您需要它,这是界面,
export interface Envio
{
idEnvio : number;
nombreDestinatario: string;
DNINIF: string;
codigoPostal: string;
direccionCompleta:string;
idEstadoEnvio: string;
numIntentosEntrega: number;
}
如果我调试,我可以看到JSON是正确的 json ok
I am reading through a Query my database, it executes perfectly the query because i can read through the console the JSON is OK but the table does not fill.
This is my component
@Component({
selector: 'app-envios',
templateUrl: './envios.component.html',
styleUrls: ['./envios.component.css']
})
export class EnviosComponent {
constructor(private conexion: ConexionService) {}
envios: Envio[];
@Input() idnum: number
ngOnInit() {
}
buscarEnvio()
{
const idnum = parseInt((document.getElementById('idenvio')as HTMLInputElement).value);
console.log(idnum);
this.conexion.consultarEnvio(idnum);
}
}
And this is my conexion service
export class ConexionService {
envio : Envio[];
private api = 'http://localhost:8080/ProyectoFinal/webapi';
consultarEnvio(id: Number)
{
const path = `${this.api}/estadoenvio/${id}`;
return this.http.get<Envio[]>(path).subscribe(envio => {
console.log(envio);
});;
}
}
This is the HTML
<div class="container-fluid">
<div class="input-group">
<input type="text" class="form-control" placeholder="CC" id="idenvio">
<span class="input-group-btn">
<button type="submit" class="btn btn-danger" type="button" (click)="buscarEnvio()">Buscar</button>
</span>
</div>
<br>
<div class="col" id="tabla">
<table class="table table-border">
<thead>
<tr class="table-danger">
<th scope="col">Id del envío</th>
<th scope="col">Nombre del destinatario</th>
<th scope="col">Dirreción de envío</th>
<th scope="col">Código Postal</th>
<th scope="col">Intentos de entrega</th>
<th scope="col">Estado del envio</th>
</tr>
</thead>
<tbody>
<tr class="table-danger" *ngFor="let envio of envios">
<td class="table-danger">{{envio.idEnvio}}</td>
<td class="table-danger">{{envio.nombreDestinatario}}</td>
<td class="table-danger">{{envio.direccionCompleta}}</td>
<td class="table-danger">{{envio.codigoPostal}}</td>
<td class="table-danger">{{envio.numIntentosEntrega}}</td>
<td class="table-danger">{{envio.idEstadoEnvio}}</td>
</tr>
</tbody>
</table>
</div>
</div>
In case you need it, this is the interface
export interface Envio
{
idEnvio : number;
nombreDestinatario: string;
DNINIF: string;
codigoPostal: string;
direccionCompleta:string;
idEstadoEnvio: string;
numIntentosEntrega: number;
}
If I debug I can see the Json is correct
JSON OK
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我看到了一些改进:
IDNUM
作为输入和const?根据我的看法,您正在尝试读取输入元素的值,以触发服务的读数。强制执行该字段是数字的,并使用2条绑定来设置idnum
的值。(单击)=“ Buscarenvio()”
触发操作,分配envios $
envioscomponent中的变量(envios $ = this.conexion.conexion.conexion.consultarenvio(idnum)< /代码>)。
envios $
将是类型obervable&lt; envio []&gt;
。之后,在HTML中使用envios $
与异步管 - &gt;*ngfor =“让Envios $ | async的Envio”
。可能是更多建议,但我认为这是一开始的。
I see a couple of improvements:
idnum
both as input and also as a const? From what I see, you are trying to read the value of an input element, based on which to trigger the reading from the service. Enforce the field to be numeric, and use 2-way binding to set the value ofidnum
.(click)="buscarEnvio()"
action is triggered, assignenvios$
variable in EnviosComponent (envios$ = this.conexion.consultarEnvio(idnum)
).envios$
will be of typeObervable<Envio[]>
. Afterwards, useenvios$
in html with async pipe -->*ngFor="let envio of envios$ | async"
.Could be more pieces of advice, but I think it's something to start with.
您没有分配对
envio
数组的任何响应。在subscribe()
中,将响应分配给envio
更新的组件:
更新服务:
You are not assigning any response to
envio
array. Insubscribe()
, assign the response toenvio
Updated Component:
Updated Service:
问题是您没有设置用于显示数据的变量
envios
(根据您的HTML)。方法
咨询
返回承诺,因此您需要在异步方法的回调中设置变量。您的代码应该看起来像:
Conexionservice:
组件:
The problem is that you are not setting your variable
envios
which is used to display the data (according to your html).The method
consultarEnvio
returns a promise so you need to set your variable in the callback of your async method.Your code should look like this:
conexionService:
Component: