-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0517521
commit 25d652a
Showing
5 changed files
with
54 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { TestBed, async, inject } from '@angular/core/testing'; | ||
|
||
import { PendingChangesGuard } from './pending-changes.guard'; | ||
|
||
describe('PendingChangesGuard', () => { | ||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
providers: [PendingChangesGuard] | ||
}); | ||
}); | ||
|
||
it('should ...', inject([PendingChangesGuard], (guard: PendingChangesGuard) => { | ||
expect(guard).toBeTruthy(); | ||
})); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { CanDeactivate } from '@angular/router'; | ||
import { Injectable } from '@angular/core'; | ||
import {Observable} from 'rxjs'; | ||
|
||
export interface ComponentCanDeactivate { | ||
canDeactivate: () => boolean | Observable<boolean>; | ||
} | ||
|
||
@Injectable() | ||
export class PendingChangesGuard implements CanDeactivate<ComponentCanDeactivate> { | ||
canDeactivate(component: ComponentCanDeactivate): boolean | Observable<boolean> { | ||
// if there are no pending changes, just allow deactivation; else confirm first | ||
return component.canDeactivate() ? | ||
true : | ||
// NOTE: this warning message will only be shown when navigating elsewhere within your angular app; | ||
// when navigating away from your angular app, the browser will show a generic warning message | ||
// see http://stackoverflow.com/a/42207299/7307355 | ||
confirm('WARNING: You have unsaved changes. Press Cancel to go back and save these changes, or OK to lose these changes.'); | ||
} | ||
} |