带Angular 的Spring控制器

3df52oht  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(317)

我的 RestController :

@RestController
public class TeacherRestController {

private static final String TEACHER_MODEL = "teacher";

@Autowired
TeacherService teacherService;

@GetMapping("/rest/teachers/getAll")
public List<Teacher> getAllTeachers() {
    return teacherService.getAll();
}
}

型号 Teacher :

@Entity
@ToString
@Setter
@Getter
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "teachers", schema = "public")
public class Teacher {
@Id
@Column(name="teacher_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int teacherId;
@Column(name = "teacher_name")
@NotNull
@Size(min = 4,max = 75,message = "Teacher name should be not less than 4 symbols and not more than 75 symbols!")
private String teacherName;
@Column(name = "position")
@NotNull
@Size(min = 3,max = 50,message = "Teacher position should be not less than 3 symbols and not more than 50 symbols!")
private String position;

我的 index.html :

以及 angular.js :

var app = angular.module("myApp", []);
app.controller('myCtrl', function ($scope, $http) {

$http.get('http://localhost:8081/rest/teachers/getAll')
    .then(function(response) {
        $scope.myWelcome = response.data;
    });
});

index.html 老师不显示,但在这个地址上 http://localhost:8081/rest/teachers/getAll 我有json的老师。

wj8zmpe1

wj8zmpe11#

看起来问题出在angularjs。看看这里https://docs.angularjs.org/tutorial/step_02
从服务器加载数据时,将其分配给 $scope.myWelcome = response.data; 然后使用访问模板中的数据 teacher. 这是没有定义的 $scope .
尝试重命名 myWelcome 进入 teacher .

相关问题