在Angular中只在条件匹配时显示值



我想显示一个hr在Angular HTML中发布的职位列表。如果一个hr登录了,那么他/她发布的所有职位都应该只显示在hr页面上,而不是所有hr发布的职位。我试图通过比较hrId与hrId(在jobPosting中用作外键)表来实现这一点。如果两者都匹配,那么该页面应该显示该特定Hr发布的所有职位。Hr id我将得到当Hr登录,并通过使用本地存储access_token。其他的我将使用一个get方法,它将拥有所有的工作。

下面是LoadCurrentUser使用本地存储的代码。从这里我将得到当前HR的Id

loadCurrentUser(){
const token=localStorage.getItem("access_item");
const userInfo=token != null ? this.jwtHelperService.decodeToken(token):null;
const data=userInfo ? {
UserId:userInfo.UserId,
UserName:userInfo.UserName,
Email:userInfo.Email,
City:userInfo.Company
}:null;
this.currentUser.next(data);
return data;
}

从Angular和。net中获取方法

getAllJobPostings():Observable<JobPosting[]>{
return this.http.get<JobPosting[]>(this.basicURL+'Main/GetAllJobPostings');
}

从。net获取方法

[HttpGet("GetAllJobPostings")]
public IEnumerable<JobPosting> GetDetails(){
return Context.JobPosting.ToList();
}

招聘启事模型类别

export class JobPosting{
jobid:number=0;
title:string='';
hrId:number=0;
experience:number=0;
position:string='';
company:string='';
ctc:number=0;
skills:string='';
description:string='';
}

数据库结果预期结果

如果HrId=2匹配,则只显示该特定记录。

请建议我如何实现这一点,建议我的代码。

可能有些人不知道我在找什么,我在这里简化一下。实际上,我想根据HR的Id来呈现值,假设我的HrID =1,那么我发布的任何工作都应该显示在HR Admin页面上。

代码


user:any;
hrid:any;
allJobDetails:JobPosting[]=[];

constructor(private service:UserServiceService){};
ngOnInit(): void {

this.user=this.service.loadCurrentUser();

this.getJobDetails();
}
getJobDetails()
{
this.service.getAllJobPostings().subscribe(
(res:JobPosting[])=>
{
for(let r of res)
{
if(r.hrId == this.user.UserId)
{
this.hrid=r.hrId.toString();
this.allJobDetails.push(r);
/* this.allJobDetails=r;  -- don't do this, if you do this then you'll get 
the error when you're trying to print using "allJobDetails" in HTMl, as this will throw the error as **typeOf(allJobDetails)** is **OBJECT** and you'll not be able to render the object in HTML.    
*/   }
}
}
)
}

下面我提到了我在HTML中使用的代码

<div *ngIf="user.UserId === hrid ">
{{user|json}}
<div *ngFor="let job of allJobDetails">
<p>{{job.title}}</p>
<p>{{job.company}}</p>
</div>
</div>

在这里,您将通过打印JobPosting模型类中的值来获得预期的结果,我也得到了我在问题中提到的预期结果.

相关内容

最新更新