Tuesday 15 July 2014

angularjs - Restrict access to route with routeprovider unless variable as been set -



angularjs - Restrict access to route with routeprovider unless variable as been set -

i'm in process of learning angularjs, working on more in-depth todo app. i'm having issue trying limit access url or "route" using angular.

when nail dev url on machine (todo.ang) brings todo.ang/#/home, on view see categories have todos associated each. eg (category = cat, cat has todo of "feed", , "play"), when click category i'm calling $scope.gotocategory function (seen in js fiddle) sets variable firebase ref redirects /#/todo. working correctly.

my problem is, don't want user able access /#/todo if todoref variable still undefined. seems after $scope.gotocategory called , todoref set firebase url, routerprovider never gets recalled know todoref has been set different value forces /#/home.

code:

var todoref = undefined; if (todoref !== undefined) { $routeprovider.when('/todo', { templateurl: 'views/todo.html', controller: 'todoctrl' }); } $scope.gotocategory = function(catid) { test = catid; todoref = new firebase("url here"); $location.path('/todo'); }

i didn't include entire file of code if thats necessary, can well.

jsfiddle

all routes beingness set during config phase. happens in code 'todo' route ignored during initiation of ngroute.

what should setup route have resolve so:

app.config(['$routeprovider', function($routeprovider) { $routeprovider.when('/todo', { templateurl: 'views/todo.html', controller: 'todoctrl', resolve: { todoref: ['$q', function($q) { homecoming todoref ? todoref : $q.reject('no ref'); }] } }); }]);

if 'todoref' undefined route rejected.

also should consider moving 'todoref' service , not on global scope.

you can hear route errors , illustration redirect home route:

app.run(['$rootscope', '$location', function($rootscope, $location) { $rootscope.$on('$routechangeerror', function() { $location.path('/home'); }); }]);

angularjs angular-ui-router

No comments:

Post a Comment