java - 我想从数据库中填充数据并使用 Angular 前端和 Spring 引导后端将其放入下拉列表中

标签 java mysql hibernate spring-boot angular5

我在 MySQL 中有一个数据库,我需要从数据库中获取数据并使用 AngularJS 填充到下拉列表中。我已经在我的 AngularJS 中为下拉列表尝试过这段代码,但它没有从数据库中获取数据并填充它。

 <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> 

This is the code which is used to get all the locations.

// 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);
  }

Spring boot back end code is like this.

@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;
        }

对使用 Angular JS 填充数据有什么建议吗?

最佳答案

一步一个脚印,首先确保调用成功。

this.http.get(url).subscribe(res => console.log(res));

打开开发者面板,在ConsoleNetwork 选项卡中,可以看到打印出你想要的任何东西。获取数据后,您可以将其分配给类属性。

数据; this.http.get(url).subscribe(res => { this.data = res });

剩下的就是如何将数据与角度模板连接起来。

关于java - 我想从数据库中填充数据并使用 Angular 前端和 Spring 引导后端将其放入下拉列表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50421641/

相关文章:

hibernate - Grails引发非唯一对象异常

java - 使用TCP时如何知道客户端收到文件

php - 将表限制为 100 行,然后删除最后一行并在顶部添加新行(MYSQL、PHP)

java - 获取 Java 文件系统文件夹 onchange 事件(如 Dropbox)

C# 在 mysql 中插入复选框的值而不使用 checklistbox

mysql - 状态字段的整数值

java - hibernate - 减少查询次数

java - hibernate 。多对多。双向关联。楼主是什么意思?

Java servlet 过滤器在登录时不起作用

java - 当应用程序部署在负载均衡器后面的多个实例上时,如何在 Java 中同步公共(public)资源