Skip to main content
cngx-src documentation

canDeactivateWhenClean

Functioncommon/interactive/guard

projects/common/interactive/guard/can-deactivate.ts

Description#

Creates a functional route guard that blocks navigation when the form is dirty.

Works with Angular's CanDeactivateFn. The isDirty callback is evaluated on each navigation attempt. When dirty, shows a confirm dialog.

Uses DOCUMENT injection for SSR safety — returns true (allow) when no window is available.

Pair with CngxBeforeUnload for full coverage (browser close + route change):

// Route config
{
  path: 'edit',
  component: EditComponent,
  canDeactivate: [canDeactivateWhenClean(() => inject(EditComponent).isDirty())]
}

Signature#

canDeactivateWhenClean(isDirty, message: string)

Parameters#

@paramisDirty
  • Callback that returns true when there are unsaved changes.
@parammessagestring= 'You have unsaved changes. Leave anyway?'
  • Confirmation message. Default: 'You have unsaved changes. Leave anyway?'

Returns#

boolean

A functional guard compatible with Angular's canDeactivate.