Angular 5:选择具有相关角色OneToMany Relationship的所有用户 [英] Angular 5 : select all users with related role OneToMany Relationship
本文介绍了Angular 5:选择具有相关角色OneToMany Relationship的所有用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试选择所有具有相关角色的用户,因为知道一个角色可以与许多联系人相关,而一个联系人只有一个角色.
I'm trying to select all users with related role, knowing that one role can be related to many contacts and one contact has one role.
我尝试了下面的代码,但是它只返回一个空白表,这是休眠还是我如何称呼公司名称列的问题?
i tried the code bellow but it return just a blank table, is it a problem with hibernate or with how i called the company name column ?
ContactRepository.java
@Query("SELECT c, cmp.name FROM Contact c, Company cmp WHERE c.company=cmp.id")
public Page<Contact> search(Pageable page);
ContactRestService
@RequestMapping(value="/searchContacts", method=RequestMethod.GET)
@ResponseBody
public Page<Contact> search(
@RequestParam(name="page",defaultValue="0") int page,
@RequestParam(name="size",defaultValue="5") int size){
return contactRepository.search(PageRequest.of(page, size));
}
contacts.component.html
<tr *ngFor="let c of pageContacts?.content">
<td class="py-1">
<img src="../../assets/images/faces-clipart/pic-1.png" alt="image"/>
</td>
<td>{{c.email}}</td>
<td>{{c.phone}}</td>
<td>{{c.firstName}}</td>
<td>{{c.lastName}}</td>
<td>{{c.job}}</td>
<td>{{c.birthday}}</td>
<td>{{c.company.name}}</td>
</tr>
contact.service.ts
@Injectable()
export class ContactsService {
constructor(private http:HttpClient){}
getContacts(){
return this.http.get("http://localhost:8080/searchContacts?page=0&size=6")
}
}
contacts.component.ts
@Component({
selector: 'app-contacts',
templateUrl: './contacts.component.html',
styleUrls: ['./contacts.component.css']
})
export class ContactsComponent implements OnInit {
pageContacts:any;
constructor(private contactsService:ContactsService) { }
ngOnInit() {
console.log("Initialisation......");
this.contactsService.getContacts()
.subscribe(data =>{
console.log(data);
this.pageContacts=data;
}, err=>{
console.log("ALERT!!!!!"+err);
})
}
}
推荐答案
实际上,响应是一个数组,因此当我将代码更改为此时,它可以工作:
In fact the response is an array so it works when i changed my code to this :
<tr *ngFor="let c of pageContacts?.content">
<td class="py-1">
<img src="../../assets/images/faces-clipart/pic-1.png" alt="image"/>
</td>
<td>{{c[0].email}}</td>
<td>{{c[0].phone}}</td>
<td>{{c[0].firstName}}</td>
<td>{{c[0].lastName}}</td>
<td>{{c[0].job}}</td>
<td>{{c[0].birthday}}</td>
<td>{{c[1]}}</td>
</tr>
这篇关于Angular 5:选择具有相关角色OneToMany Relationship的所有用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文