Java 我想从数据库中填充数据,并使用Angular Front end和Spring boot back end将其放在下拉列表中
我在MySQL中有一个数据库,我需要从数据库中获取数据,并使用AngularJS在下拉列表中填充。我在AngularJS中尝试了这段代码,但它没有从数据库中获取数据并进行填充Java 我想从数据库中填充数据,并使用Angular Front end和Spring boot back end将其放在下拉列表中,java,mysql,hibernate,spring-boot,angular5,Java,Mysql,Hibernate,Spring Boot,Angular5,我在MySQL中有一个数据库,我需要从数据库中获取数据,并使用AngularJS在下拉列表中填充。我在AngularJS中尝试了这段代码,但它没有从数据库中获取数据并进行填充 <div data-ng-init="getLocation1()"> <b>Current Location:</b> <select id="cur_location"> <optio
<div data-ng-init="getLocation1()">
<b>Current Location:</b> <select id="cur_location">
<option value="">-- Select Current Locations --</option>
<option data-ng-repeat="location1 in location"
value="{{location1.name}}">{{location1.name}}</option>
</select><br>
</div>
当前位置:
--选择当前位置--
{{location1.name}
这是用于获取所有位置的代码
//获取所有位置1
getLocation1():承诺{
返回this.http.get(this.location1Url)
.toPromise()
.then(response=>response.json()作为位置1[])
.接住(这个.把手错误);
}
getLocation1ByName(名称:string):承诺{
常量url=`findbyname/${name}`;
返回此.http.get(url)
.toPromise()
.then(response=>response.json()作为位置1)
.接住(这个.把手错误);
}
Spring boot后端代码如下所示
@GetMapping(value=“/location1”,products=MediaType.APPLICATION\u JSON\u value)
公共列表getAll(){
列表=新的ArrayList();
Iterable location=repository.findAll();
location.forEach(列表::add);
退货清单;
}
@后映射(value=“/postlocation1”)
公共位置postLocation(@RequestBody-Location){
save(新位置(Location.getName(),Location.getX(),Location.getY());
返回位置;
}
任何使用Angular JS填充数据的建议?一次一步,首先确保调用成功
this.http.get(url.subscribe(res=>console.log(res));
打开“开发人员”面板,在控制台
和网络
选项卡中查看所需的任何打印内容。获得数据后,可以将其分配给类属性
数据;
this.http.get(url.subscribe)(res=>{this.data=res});
剩下的就是如何用角度模板连接数据
// Get all location1
getLocation1(): Promise<Location1[]> {
return this.http.get(this.location1Url)
.toPromise()
.then(response => response.json() as Location1[])
.catch(this.handleError);
}
getLocation1ByName(name: string): Promise<Location1[]> {
const url = `findbyname/${name}`;
return this.http.get(url)
.toPromise()
.then(response => response.json() as Location1)
.catch(this.handleError);
}
@GetMapping(value="/location1", produces= MediaType.APPLICATION_JSON_VALUE)
public List<Location> getAll() {
List<Location> list = new ArrayList<>();
Iterable<Location> location = repository.findAll();
location.forEach(list::add);
return list;
}
@PostMapping(value="/postlocation1")
public Location postLocation(@RequestBody Location location) {
repository.save(new Location(location.getName(),location.getX(),location.getY()));
return location;
}