如果管理员未登录,您可以过滤 ngFor 以不显示管理员用户吗?
if an admin is not logged in Can you filter a ngFor to not show the Admin users.
我从数据库中提取用户列表,用户列表中有管理员用户和非管理员用户。我想在管理员登录时显示管理员用户,如果没有管理员登录则不显示管理员用户。这是我当前的代码:
<tbody>
<tr *ngFor="let user of users">
<td>{{user.Salutation.Description === "None" ? "-" : user.Salutation.Description}}</td>
<td>{{user.FirstName}}</td>
<td>{{user.Surname}}</td>
<td>{{user.Region.Description === "None" ? "-" : user.Region.Description}}</td>
<td>{{user.Institution === null ? "-" : user.Institution}}</td>
<td>{{user.PrimaryResearchField.Description === "None" ? "-" : user.PrimaryResearchField.Description}}</td>
<td>{{user.OrcidID === null ? "-" : user.OrcidID}}</td>
<td>
<button title="View" class="btn btn-primary" (click)="viewUser(user.UserKey)">View</button>
<button *ngIf="isAdmin || (isSi && userKey == user.UserKey)" title="Edit" class="btn btn-success" (click)="editUser(user.UserKey)">Edit</button>
<button *ngIf="isAdmin" title="Delete" class="btn btn-danger" (click)="deleteUser(user.UserKey)">Delete</button>
</td>
</tr>
</tbody>
提前致谢。
简短:
<ng-template *ngFor="let user of users">
<tr *ngIf="isAdmin && user.isAdmin"> // check all your conditions here
.....
</tr>
</ng-template>
Another way is :
1) Filter users array from component side on ngOnInit
as per the
logged in user
2) Use pipe function to filter the array
感谢 Vivek Doshi 的帮助。我设法通过一些调整让它显示正确的信息。使用 <ng-template>
不起作用 table 中不会显示任何内容,所以我将 *ngFor
添加到 <tbody>
然后我添加了以下行 <tr *ngIf="isAdmin && (user.TestRole.Description === 'Admin' || user.TestRole.Description === 'Senior Investigators') || user.TestRole.Description === 'Senior Investigators'">
然后加载所有如果管理员登录则为用户,如果非管理员用户登录则为非管理员用户。
我从数据库中提取用户列表,用户列表中有管理员用户和非管理员用户。我想在管理员登录时显示管理员用户,如果没有管理员登录则不显示管理员用户。这是我当前的代码:
<tbody>
<tr *ngFor="let user of users">
<td>{{user.Salutation.Description === "None" ? "-" : user.Salutation.Description}}</td>
<td>{{user.FirstName}}</td>
<td>{{user.Surname}}</td>
<td>{{user.Region.Description === "None" ? "-" : user.Region.Description}}</td>
<td>{{user.Institution === null ? "-" : user.Institution}}</td>
<td>{{user.PrimaryResearchField.Description === "None" ? "-" : user.PrimaryResearchField.Description}}</td>
<td>{{user.OrcidID === null ? "-" : user.OrcidID}}</td>
<td>
<button title="View" class="btn btn-primary" (click)="viewUser(user.UserKey)">View</button>
<button *ngIf="isAdmin || (isSi && userKey == user.UserKey)" title="Edit" class="btn btn-success" (click)="editUser(user.UserKey)">Edit</button>
<button *ngIf="isAdmin" title="Delete" class="btn btn-danger" (click)="deleteUser(user.UserKey)">Delete</button>
</td>
</tr>
</tbody>
提前致谢。
简短:
<ng-template *ngFor="let user of users">
<tr *ngIf="isAdmin && user.isAdmin"> // check all your conditions here
.....
</tr>
</ng-template>
Another way is :
1) Filter users array from component side on
ngOnInit
as per the logged in user2) Use pipe function to filter the array
感谢 Vivek Doshi 的帮助。我设法通过一些调整让它显示正确的信息。使用 <ng-template>
不起作用 table 中不会显示任何内容,所以我将 *ngFor
添加到 <tbody>
然后我添加了以下行 <tr *ngIf="isAdmin && (user.TestRole.Description === 'Admin' || user.TestRole.Description === 'Senior Investigators') || user.TestRole.Description === 'Senior Investigators'">
然后加载所有如果管理员登录则为用户,如果非管理员用户登录则为非管理员用户。