I'm trying to stop navigation on my application if any form made changes and try to navigate without saving changes.
i want to show a dialog to show whether to save navigate or discard or cancel navigation action.
i'm using angular ui.routing
app.config(function($stateProvider, $urlRouterProvider) {
$urlRouterProvider.otherwise('/dashboard');
$stateProvider
.state('dashboard', {
url: '/dashboard',
templateUrl: '/application/private/views/dashboard.html'
})
.state('websites', {
url: '/websites',
templateUrl: '/application/private/views/websites.html'
})
});
i was planning to have implementation something like using angularjs service singleton
app.service('dirtyCheckService', function ($rootScope, dialogService){
});
and on controller level i can write a submit click like this
app.controller('formSubmitCtrl', function ($scope, dirtyCheckService){
dirtyCheckService.checkForm($scope).then(function(){
//allow navigation
},function(){
//not allowed}
});
i'm not sure is there easy way exist already or not
thanks you
The answer is: It is ok to move that kind of check into service/factory - for some further reuse. Also, here is some example at least to show how to - plunker
A factory:
And having a
view
like this:And the controller:
There is an example
You're right about a service being the way to do this, something like this should do:
Then maybe somewhere in your app, such as the run block:
Of course the above will only work given that you set your form to pristine once the user saves their changes.