正如标题中所提到的,我想在角度4中执行search
函数。我在SpringBoot后端有REST服务,我在Postman中调用了它,它工作得很好但是在前面 - 它没有角度。
我在Service.TS
@Injectable()
export class EvenementService {
private evensUrl_del = 'http://localhost:8080/evenement';
private headers = new Headers({ 'Content-Type': 'application/json' });
private options = new RequestOptions({ headers: this.headers });
constructor(private http: Http) { }
// Get all events
getEvenement(): Promise<Evenement[]> {
return this.http.get(this.evensUrl)
.toPromise()
.then(response => response.json() as Evenement[])
.catch(this.handleError);
}
getEvenementByType(typeE: string): Promise<Evenement[]> {
return this.http.get(this.evensUrl_del + '/evenements/' + typeE, this.options)
.toPromise()
.then(response => response.json() as Evenement)
.catch(this.handleError);
} }
我的Component.TS
@Component({
selector: 'app-evenement',
templateUrl: './evenement.component.html',
styleUrls: ['./evenement.component.css']
})
export class EvenementComponent implements OnInit {
events: Evenement = new Evenement();
evenmt: Evenement[];
//e: Evenement[];
typeE: string;
constructor(public router: Router, private modalService: NgbModal, private evenementService: EvenementService, private toastyService: ToastyService) { }
ngOnInit(): void {
this.typeE="";
//this.getEvenements();
}
ver(events: Evenement, modal) {
evenmt => this.evenmt = evenmt;
this.modalService.open(modal, { size: 'lg' });
}
searchEvenement(){
this.evenementService.getEvenementByType(this.typeE).then(evenmt => this.evenmt = evenmt);
}
getEvenements() {
this.evenementService.getEvenement().then(evenmt => this.evenmt = evenmt);
}
}
最后是component.html
<div style="width: 300px;">
<div class="form-group">
<label for="typeE"><b>Type</b></label>
<input type="text" class="form-control" id="typeE" name="typeE" [(ngModel)]="typeE" required>
</div>
<div class="btn-group">
<button type="button" class="btn btn-success" (click)="searchEvenement()">Rechercher</button>
</div>
</div>
<ul>
<li *ngFor="let e of evenmt">
<h4>{{e.typeE}} - {{e.prixE}} {{e.dateE}}</h4>
</li>
</ul>
使用上面的代码,当我点击浏览器中的搜索按钮时,会返回以下错误:
错误错误:未捕获(在承诺中):响应状态:302确定URL:http://localhost:8080/evenement/evenements/degustation 尽管在eclipse终端中显示我要查找的行。所以我认为这个问题实际上是在Angular,但我不知道在哪里兴奋。
有人可以帮我解决这个问题吗?感谢
答案 0 :(得分:0)
您无需承诺可以将代码修改为:
<强>服务强>
@Injectable()
export class EvenementService {
private evensUrl_del = 'http://localhost:8080/evenement';
private headers = new Headers({ 'Content-Type': 'application/json' });
private options = new RequestOptions({ headers: this.headers });
constructor(private http: Http) { }
// Get all events
getEvenement(): Promise<Evenement[]> {
return this.http.get(this.evensUrl)
.map(res => res.json())
.catch((err) => this.handleError(err));
}
getEvenementByType(typeE: string): Promise<Evenement[]> {
return this.http.get(`${this.evensUrl_del}/evenements/${typeE}`, this.options)
.map(res => res.json())
.catch((err) => this.handleError(err));
}
}
<强>组件强>
searchEvenement(){
this.evenementService.getEvenementByType(this.typeE).subscribe(evenmt => {
setTimeout(() => { <====== You can use timeout
this.evenmt = evenmt;
});
});
}
getEvenements() {
this.evenementService.getEvenement().subscribe(evenmt => this.evenmt = evenmt);
}