Angular 当详细信息为空时,不显示空表单
在这里,我试图显示一个名为custominfo的表单,其中有两个案例很重要 案例1:当自定义信息与详细信息一起出现在数据库显示表单中时。 案例2:当数据库中没有自定义信息详细信息时,显示空表单。 这里我得到了案例1,但是我没有得到案例2,表单没有显示,我为此做了很多尝试,但是没有用,请有人帮我解决这个问题。 我的服务.tsAngular 当详细信息为空时,不显示空表单,angular,typescript,Angular,Typescript,在这里,我试图显示一个名为custominfo的表单,其中有两个案例很重要 案例1:当自定义信息与详细信息一起出现在数据库显示表单中时。 案例2:当数据库中没有自定义信息详细信息时,显示空表单。 这里我得到了案例1,但是我没有得到案例2,表单没有显示,我为此做了很多尝试,但是没有用,请有人帮我解决这个问题。 我的服务.ts import { Injectable } from '@angular/core'; import { Http, Response } from '@angular/ht
import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import { IDetails } from './details';
@Injectable()
export class GetCustInfo {
str = localStorage.getItem('social');
loc = JSON.parse(this.str);
id = this.loc.profile_id;
private _productUrl = 'http://localhost/a2server/index.php/profile/editcustominfo/'+this.id;
constructor(private _http: Http) { }
getCustList(): Observable<IDetails[]> {
return this._http.get(this._productUrl)
.map((response: Response) => { return <IDetails[]> response.json().data[0];
});
}
}
我的模板
<div class="col-sm-12 nopadding custominfo contenttoppadding">
<form class="nobottommargin" *ngFor="details" [formGroup]="form" (ngSubmit)="onSubmit(form.value)" name="template-contactform" action="include/sendemail.php" method="post" novalidate="novalidate">
<div class="form-group">
<div class="input-group divcenter">
<span class="input-group-addon noradius inputgroupaddon"><i class="icon-facebook"></i></span>
<input type="email" type="email" tooltip="Custom url" [tooltipDisabled]="false" [tooltipAnimation]="true"
tooltipPlacement="top" name="widget-subscribe-form-email" [formControl]="form.controls['custominfo']" [(ngModel)]="details.custominfo" class="form-control required email formcontrolheight" placeholder="Facebook" aria-required="true" />
</div>
</div>
</form>
</div>
在这里,即使值为空,我也尝试显示结果,但当详细信息为空时,我无法看到表单。是否要显示一个表单?还是多种形式? 您正在使用
*ngFor
?!
变量details
是一个数组,但是getCustList()
函数只返回第一个元素
无论如何。。将错误处理添加到您的getCustList()订阅中
:
this._service.getCustList()
.subscribe(details => this.details = details,
err => {
this.details = [{}]; /* put your empty/default model here */
console.log(err);
}
);
你想出示一张表格吗?还是多种形式? 您正在使用
*ngFor
?!
变量details
是一个数组,但是getCustList()
函数只返回第一个元素
无论如何。。将错误处理添加到您的getCustList()订阅中
:
this._service.getCustList()
.subscribe(details => this.details = details,
err => {
this.details = [{}]; /* put your empty/default model here */
console.log(err);
}
);
this.\u service.getCustList().subscribe(details=>this.details=details);err=>{this.details=[];/*将空/默认模型放在这里*/console.log(err);}Mxii,我是这样写的,但是表单没有显示。你能解释一下空的或默认的模型是什么意思吗?请澄清你是否想显示一个或多个表单?只有一个表单…并且当结果为空时该表单不显示。请将
*ngFor=“details”
更改为*ngIf=“details”
和这个错误案例:this.details={}
和您的details变量不应是数组。this.\u service.getCustList().subscribe(details=>this.details=details);err=>{this.details=[];/*将空/默认模型放在这里*/console.log(err);}Mxii,我是这样写的,但是表单没有显示。你能解释一下空的或默认的模型是什么意思吗?请澄清你是否想显示一个或多个表单?只有一个表单…并且当结果为空时该表单不显示。请将*ngFor=“details”
更改为*ngIf=“details”
和这个错误案例:this.details={}
和您的details变量不应是数组。
this._service.getCustList()
.subscribe(details => this.details = details,
err => {
this.details = [{}]; /* put your empty/default model here */
console.log(err);
}
);