【发布时间】:2019-04-18 08:26:23
【问题描述】:
我的主要问题是,我的组件在输入数据后没有刷新 UI。我总是使用刷新页面来查看更改。我认为.subscribe 有任何问题。我使用 Angular 6。在此之前,当我使用 pure And 2 env 时效果很好。
我的组件:
ngOnInit() {
this.company = [];
this.inputCompanyService.getAllCompany().subscribe(company => {
this.company = company;
});
}
postCompany(event, companyName, ) {
var result;
var newCompany = {
company_name: companyName.value,
id: companyName.id
};
result = this.inputCompanyService.postCompany(newCompany);
result.subscribe(x => {
this.company.push(newCompany);
companyName.value = '';
});
}
我的服务:
getAllCompany() {
return this._http.get('http://localhost:3000/api/companys')
.map(res => res.json());
}
postCompany(company_name) {
var headers = new Headers();
headers.append('Content-Type', 'application/json');
return this._http.post('http://localhost:3000/api/company', JSON.stringify(company_name), { headers: headers })
.map(res => res.json());
}
HTML:
<div class="input-company">
<p>
<mat-form-field appearance="legacy">
<input matInput [(ngModel)]="companyName.company_name" placeholder="Here..." autofocus #companyName>
<!-- <mat-icon matSuffix>sentiment_very_satisfied</mat-icon> -->
</mat-form-field>
<button mat-raised-button color="warn" (click)="postCompany($event, companyName)">Click me !</button>
</p>
<div *ngFor="let company of company.company">
<li>
<button mat-flat-button (click)="deleteCompany(company)">X</button>
<button mat-flat-button>N</button>
<button mat-stroked-button>{{company.id}}-{{company.company_name}}</button>
</li>
</div>
</div>
【问题讨论】:
-
吹毛求疵:这条线
<div *ngFor="let company of company.company">看起来很糟糕。您在这里使用了三个具有相同名称的变量/属性。
标签: angular typescript