Creates a functional route guard that blocks navigation when the form is dirty
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())]
}