我有一个webapp和两个要使用的模型。一种模型是Trainees,另一种模型是TraineeStatus。我想在Trainees中获取TraineeStatus的数组或列表,然后将其传递给AngularJS,然后在select方法中显示它,什么是最好的方法?
Trainees.java
@Entity
public class Trainees {
@Id
@GeneratedValue
private int traineesID;
private int groupsID;
@ManyToOne
private TraineeStatus traineestatus;
private int customersID;
private String name;
private String surname;
private String phoneDetails;
private String email;
public Trainees(){
}
public Trainees(String name, String surname, String phoneDetails, String email, int id, int groupsID, TraineeStatus traineestatus, int customersID) {
super();
this.name = name;
this.surname = surname;
this.email = email;
this.phoneDetails = phoneDetails;
this.groupsID = groupsID;
this.traineestatus = traineestatus;
this.customersID = customersID;
}
//getters and setters
@Override
public boolean equals(Object object) {
if (object instanceof Trainees){
Trainees contact = (Trainees) object;
return contact.traineesID == traineesID;
}
return false;
}
@Override
public int hashCode() {
return traineesID;
}
}
TraineeStatus.java
@Entity
@Table(name="traineeStatus")
public class TraineeStatus {
@Id
@GeneratedValue
private int traineeStatusId;
private String value;
public TraineeStatus(){
}
public TraineeStatus(String value, int id) {
super();
this.value = value;
}
//getters and setters
@Override
public boolean equals(Object object) {
if (object instanceof TraineeStatus){
TraineeStatus value = (TraineeStatus) object;
return value.traineeStatusId == traineeStatusId;
}
return false;
}
@Override
public int hashCode() {
return traineeStatusId;
}
}
我的看法:
<div class="input-append">
<select
required
ng-model="contact.traineestatus"
name="traineestatus.id"
placeholder="<spring:message code='sample.status'/> ">
<option ng-repeat="trainee in contact.traineestatus" value="{{trainee.id}}">{{trainee.value}}</option>
</select>
</div>
<div class="input-append">
<label>
<span class="alert alert-error"
ng-show="displayValidationError && updateContactForm.traineestatus.id.$error.required">
<spring:message code="required"/>
</span>
</label>
</div>
当前与contact.traineestatus返回一个具有ID和值的对象,但我想获取一个数组。
我正在尝试利用该对象,因此我将拥有一个选择功能,该功能将显示该值并保存ID并将其保存在Trainees表中。
有没有办法利用这个对象并在我的选择中使用它?谢谢问候!
最佳答案
这有点难以解释,所以我将尽量保持简单。
首先,您需要记住,在具有联系人/学员列表的同一请求中,将不会检索TraineeStatus
的列表,您将需要一个单独的HTTP请求来获取该列表。
您具有ContactRepository
来获取所有Trainees
记录的方式相同,您将需要TraineeStatusRepository
来获取所有TraineeStatus
记录。然后,在您的一项服务上,您将需要一种方法来获取TraineeStatus
列表,如下所示:
private List<TraineeStatus> findAllTraineeStatus() {
return traineeStatusRepository.findAll();
}
之后,您将需要定义一个HTTP API来访问该列表(即GET http://example.com/traineeStatus)。这个想法可能与您检索ContactList所用的想法相同。
然后,在AngularJS应用上,您需要使用一个函数来加载
TraineeStatus
列表。例如:$scope.getTraineeStatus = function () {
// assuming the HTTP API is: GET http://example.com/traineeStatus
$http.get('http://example.com/traineeStatus', config)
.success(function (data) {
$scope.traineeStatus = data;
})
.error(function (err) {
// handle error
});
}
然后循环遍历
$scope.traineeStatus
上的ng-repeat
属性:<select
required
ng-model="contact.traineestatus"
name="traineestatus.id"
placeholder="<spring:message code='sample.status'/> ">
<option ng-repeat="status in traineeStatus" value="{{status.id}}">{{status.value}}</option>
</select>