Angular 4 更新嵌套字段
Angular 4 update nested field
使用 @angular/http method http.put()
更新我的模型时遇到问题。
问题是我根本无法更新位置。我可以成功更新任何其他字段,并且可以在使用 POST.
创建时设置任何位置
我的 angular 版本是“^4.3.3”
在 java 我的模型看起来像
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
Long id;
String name;
String email;
String phone;
Date birthDay;
@JsonBackReference
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "position_id")
Position position;
}
投影:
public interface EmployeeProjection {
Long getId();
String getName();
String getEmail();
String getPhone();
Date getBirthDay();
@Value("#{target.position.name}")
String getPosition();
}
位置class:
public class Position {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
Long id;
String name;
}
在angular模板位置:
<md-select mdInput placeholder="Position"
[(ngModel)]="newEmployee.position">
<md-option *ngFor="let position of positions | async" [value]="position.name">{{ position.name }}
</md-option>
</md-select>
我在组件中的更新方法:
update() {
let positionName = this.employee.position;
this.positionService.findByName(positionName).subscribe(position => {
this.employee.position = position._links.self.href;
this.employeeService.update(this.employee);
this.employee.position = positionName;
this.employeeService.localStorageService.set('employee', this.employee);
});
}
在役:
update(employee: Employee) {
this.http.put(employee._links.self.href, employee)
.map((resp: Response) => resp.json())
.subscribe(() => {
this.getAll();
});
return employee;
}
在chrome请求中:
{
"name": "Nikolai Morgan",
"id": 1,
"position": "http://localhost:9080/api/positions/5",
"birthDay": "1986-07-01",
"email": "NikolaiMorgan@gmail.com",
"phone": "+380840713229",
"_links": {
"self": {
"href": "http://localhost:9080/api/employees/1"
},
"employee": {
"href": "http://localhost:9080/api/employees/1{?projection}",
"templated": true
},
"position": {
"href": "http://localhost:9080/api/employees/1/position"
}
}
}
但响应和预览不包含字段位置:
{
"id" : 1,
"name" : "Nikolai Morgan",
"email" : "NikolaiMorgan@gmail.com",
"phone" : "+380840713229",
"birthDay" : "1986-07-01",
"_links" : {
"self" : {
"href" : "http://localhost:9080/api/employees/1"
},
"employee" : {
"href" : "http://localhost:9080/api/employees/1{?projection}",
"templated" : true
},
"position" : {
"href" : "http://localhost:9080/api/employees/1/position"
}
}
}
要更新引用另一个实体的实体,在 Spring Data REST 中,您必须对该实体使用 link。例如:
@Entity
class Employee {
//...
String name;
@ManyToOne
Position position;
//...
}
@Entity
class Position {
//...
String name;
//...
}
interface EmployeeRepo extends JpaRepository<Employee, Long> {}
interface PositionRepo extends JpaRepository<Position, Long> {}
首先我们添加一个职位:
POST http://localhost:9080/api/positions
{
"name": "position1"
}
并得到这样的回复:
{
"name": "position1",
"_links" : {
"self" : {
"href" : "http://localhost:9080/api/positions/1"
},
"position" : {
"href" : "http://localhost:9080/api/positions/1"
}
}
}
然后添加员工:
POST http://localhost:9080/api/employees
{
"name": "employee1",
"employee": "http://localhost:9080/api/positions/1"
}
然后得到回复:
{
"name" : "employee1",
"_links" : {
"self" : {
"href" : "http://localhost:9080/api/employees/1"
},
"employee" : {
"href" : "http://localhost:9080/api/employees/1",
},
"position" : {
"href" : "http://localhost:9080/api/employees/1/position"
}
}
}
因此,如果我们需要更新职位,我们会创建一个新职位,然后 PUT 员工:
PUT http://localhost:9080/api/employees/1
{
"name": "employee1",
"position": "http://localhost:9080/api/positions/2"
}
甚至修补它:
PATCH http://localhost:9080/api/employees/1
{
"position": "http://localhost:9080/api/positions/2"
}
使用 @angular/http method http.put()
更新我的模型时遇到问题。
问题是我根本无法更新位置。我可以成功更新任何其他字段,并且可以在使用 POST.
创建时设置任何位置我的 angular 版本是“^4.3.3”
在 java 我的模型看起来像
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
Long id;
String name;
String email;
String phone;
Date birthDay;
@JsonBackReference
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "position_id")
Position position;
}
投影:
public interface EmployeeProjection {
Long getId();
String getName();
String getEmail();
String getPhone();
Date getBirthDay();
@Value("#{target.position.name}")
String getPosition();
}
位置class:
public class Position {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
Long id;
String name;
}
在angular模板位置:
<md-select mdInput placeholder="Position"
[(ngModel)]="newEmployee.position">
<md-option *ngFor="let position of positions | async" [value]="position.name">{{ position.name }}
</md-option>
</md-select>
我在组件中的更新方法:
update() {
let positionName = this.employee.position;
this.positionService.findByName(positionName).subscribe(position => {
this.employee.position = position._links.self.href;
this.employeeService.update(this.employee);
this.employee.position = positionName;
this.employeeService.localStorageService.set('employee', this.employee);
});
}
在役:
update(employee: Employee) {
this.http.put(employee._links.self.href, employee)
.map((resp: Response) => resp.json())
.subscribe(() => {
this.getAll();
});
return employee;
}
在chrome请求中:
{
"name": "Nikolai Morgan",
"id": 1,
"position": "http://localhost:9080/api/positions/5",
"birthDay": "1986-07-01",
"email": "NikolaiMorgan@gmail.com",
"phone": "+380840713229",
"_links": {
"self": {
"href": "http://localhost:9080/api/employees/1"
},
"employee": {
"href": "http://localhost:9080/api/employees/1{?projection}",
"templated": true
},
"position": {
"href": "http://localhost:9080/api/employees/1/position"
}
}
}
但响应和预览不包含字段位置:
{
"id" : 1,
"name" : "Nikolai Morgan",
"email" : "NikolaiMorgan@gmail.com",
"phone" : "+380840713229",
"birthDay" : "1986-07-01",
"_links" : {
"self" : {
"href" : "http://localhost:9080/api/employees/1"
},
"employee" : {
"href" : "http://localhost:9080/api/employees/1{?projection}",
"templated" : true
},
"position" : {
"href" : "http://localhost:9080/api/employees/1/position"
}
}
}
要更新引用另一个实体的实体,在 Spring Data REST 中,您必须对该实体使用 link。例如:
@Entity
class Employee {
//...
String name;
@ManyToOne
Position position;
//...
}
@Entity
class Position {
//...
String name;
//...
}
interface EmployeeRepo extends JpaRepository<Employee, Long> {}
interface PositionRepo extends JpaRepository<Position, Long> {}
首先我们添加一个职位:
POST http://localhost:9080/api/positions
{
"name": "position1"
}
并得到这样的回复:
{
"name": "position1",
"_links" : {
"self" : {
"href" : "http://localhost:9080/api/positions/1"
},
"position" : {
"href" : "http://localhost:9080/api/positions/1"
}
}
}
然后添加员工:
POST http://localhost:9080/api/employees
{
"name": "employee1",
"employee": "http://localhost:9080/api/positions/1"
}
然后得到回复:
{
"name" : "employee1",
"_links" : {
"self" : {
"href" : "http://localhost:9080/api/employees/1"
},
"employee" : {
"href" : "http://localhost:9080/api/employees/1",
},
"position" : {
"href" : "http://localhost:9080/api/employees/1/position"
}
}
}
因此,如果我们需要更新职位,我们会创建一个新职位,然后 PUT 员工:
PUT http://localhost:9080/api/employees/1
{
"name": "employee1",
"position": "http://localhost:9080/api/positions/2"
}
甚至修补它:
PATCH http://localhost:9080/api/employees/1
{
"position": "http://localhost:9080/api/positions/2"
}