I am building an app where user must be loggedin/signup from the beginning. The one must be loggedin to see a single view , I have to put below code on every view
var post = JSON.parse(window.localStorage['chanmeds_cridentials'] || '{}'); if ( post.username !== undefined && post.loggedin !== undefined ){ $scope.alreadyloggedin = true; } else { $scope.alreadyloggedin = false; //$state.go("app.playlists"); //$location.path("app.playlists"); }
One, I have repeat code again and again on every controller and second , either $state.go(âapp.playlistsâ); or $location.path(âapp.playlistsâ); (which I commented in above code) nothing redirecting to login page well, while testing on my mac. some kind of error I am not aware of , so instead of auto redirect to login view (which is playlists for now ) , I have to do trick below on every view :
Do I have to ? so instead of
controller(âAppCtrlâ, function($scope, $http)
it should be like
controller(âAppCtrlâ, function($scope, $http, $state, $location) ?
However, last thing remain, do i have to paste the code in every view ? I guess, there should be any function or service stuff instead of pasting same codes.
yes , you can maintain the same code in a single service and call the service function from the controller ! which will require injecting the service name in the controller ! google angular services or google sharing data between controllers using service