Conditional Rendering
Overview
Two structural directives render a template based on the current user’s permissions, letting an Angular application conditionally show or hide parts of its UI:
-
ocxIfPermission— renders the template only when the user *has the required permission(s). -
ocxIfNotPermission— renders the template only when the user does *not have the required permission(s), which is useful for showing a fallback or hint to users without access.
Both accept a single permission (a string) or several (an array). When an array is given, all of the listed permissions are required — the check is a logical AND.
|
The directives resolve the current user’s permissions through the |
*ocxIfPermission
Shows the host template only if the user has the required permission(s).
<button pButton pRipple *ocxIfPermission="'USER#EDIT'">
{{ 'ACCESS_PAGE_KEY.EDIT' | translate }}
</button>
<button pButton pRipple *ocxIfPermission="['USER#VIEW', 'USER#EXPORT']">
{{ 'ACCESS_PAGE_KEY.EXPORT' | translate }}
</button>
*ocxIfNotPermission
Shows the host template only if the user does not have the required permission(s). This is useful for displaying fallback UI (a hint or read-only message) to users without access.
<div class="text-muted" *ocxIfNotPermission="'USER#VIEW'">
{{ 'ACCESS_PAGE_KEY.NO_ACCESS_HINT' | translate }}
</div>
Microsyntax options
Beyond show/hide, both directives support rendering an alternative elseTemplate and keeping the UI visible but disabled when the permission is missing (onMissingPermission: 'disable'). Both directives support the same microsyntax options:
| Option | Type | Meaning |
|---|---|---|
|
|
Template to render when the permission condition is not met. |
|
|
Default is |
|
|
Optional override list of permissions to use for the check (bypasses the injected checker/service). |
elseTemplate
Renders an alternative template when the permission condition is not met.
<button
pButton
pRipple
*ocxIfPermission="'USER#EDIT'; elseTemplate: noEditPermission"
>
{{ 'ORDER.EDIT' | translate }}
</button>
<ng-template #noEditPermission>
<p class="text-muted">{{ 'ACCESS_PAGE_KEY.READ_ONLY' | translate }}</p>
</ng-template>
For ocxIfNotPermission, the semantics are inverted: elseTemplate is rendered when the user *does have the permission(s).
onMissingPermission: 'disable'
Keeping the UI visible but disabled is useful for discoverability — the user can see the control but not interact with it.
<button
pButton
pRipple
*ocxIfPermission="'USER#DELETE'; onMissingPermission: 'disable'"
>
{{ 'ORDER.DELETE' | translate }}
</button>
|
The directive adds and removes the |
Related
-
Permissions Handling[Permissions Handling] — the feature overview, how permissions reach an application, and the
<RESOURCE>#<ACTION>convention. -
Checking permissions in code (Angular)[Checking permissions in code (Angular)] — the
PermissionServiceandHAS_PERMISSION_CHECKERchecks for logic outside a template. -
User Service — the
@onecx/angular-integration-interfaceservice the directives resolve permissions through. -
Checking permissions in code (React)[Checking permissions in code (React)] — reading permissions in a React application with
usePermission/PermissionProvider.