angularjs - Check in every controller, can it be converted in reusable module? -
i working on angular app has around 3-4 controllers. on each controller need check several things authentication / compatible browsers , redirect error controller. right now, have code block @ start of every controller..
i have ui router in place.. code block @ start of every controller.
help me best possible way make code modular..
if (is.ios() || is.windowsphone()) { $scope.errorlog = { 'message': 'this app not compatible browser using', 'time': new date() }; $state.go('error', { errorlog: $scope.errorlog }); return; }
assuming is
service or factory. mentioned use ui router makes task pretty easy.
.state('onlyandroidandwindows', { url: '/hateiphone', platforms: ['android', 'windows'] //add state platform needs checked });
then @ run stage insert route change listener
app.run(function($rootscope, $state, is) { $rootscope.$on('$statechangestart', function(event, next) { var currentplatform = is.currentplatform(); var allowedplatforms = (next.platforms) ? next.platforms : is.allplatforms(); if (allowedplatforms.indexof(currentplatform) === -1) { event.preventdefault(); var errorlog = { 'message': 'this app not compatible browser using', 'time': new date() }; $state.go('error', { errorlog: errorlog }); } }); ...
Comments
Post a Comment