-
Notifications
You must be signed in to change notification settings - Fork 42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Will this work with $routeProvider resolve functions? #23
Comments
Move angular.module('myApp')
.factory('authFilter', function ($location, userSession) {
return function () {
if (!userSession.isAuthenticated()) {
$location.path('/user/login');
return false;
}
return true;
};
});
angular.module('myApp')
.config(function ($routeProvider, authFilter) {
$routeProvider
.when('/', {
templateUrl: 'views/home.html'
})
.when('/admin', {
templateUrl: 'views/admin.html',
resolve: { authFilter: authFilter }
})
.when('/user/login', {
templateUrl: 'views/user_login.html',
controller: 'UserLoginCtrl'
})
.otherwise({
redirectTo: '/'
});
}); This not only solves the problem, but in general it's best to avoid global variables. |
Apologies, that was me just being stupid. I should have thought to refactor the code into a service; makes perfect sense. 😳 |
Alas, upon trying this out, it doesn't seem to work. I get the following error: Then someone reminded me that the only thing that can be injected into a angular.module('myApp')
.provider('authFilter', function ($location, userSession) {
this.$get = function () {
return function () {
if (!userSession.isAuthenticated()) {
$location.path('/user/login');
return false;
}
return true;
};
};
}); but then I just get Can't seem to win with this one. :\ |
Can you check if |
@btford Yeah, it is being annotated correctly; it's not an ngMin issue any more. Just can't figure out how else to do it. :/ |
Weird. If you have more info on this, please let me know and I'll re-open this issue. As of now, it seems like the problem is outside of |
I came across this issue when I had the exact same problem, and after much searching and trying different things, found the solution: Instead of
It should be just
and insted of
it should be
I worked it out after reading http://stackoverflow.com/a/16322169/67675 and the docs about $routeProvider, specifically with regards to the resolve parameter: http://docs.angularjs.org/api/ng.$routeProvider Hope this helps @Siyfion :) |
@Youdaman is right on the money. As an additional note, to inject angular.module('myApp')
.provider('authFilter', function () {
this.$get = function ($location, userSession) {
return function () {
if (!userSession.isAuthenticated()) {
$location.path('/user/login');
return false;
}
return true;
};
};
}); ngmin will also annotate these. Of course, now that Youdaman has shown how to use the service with |
Thanks @BinaryMuse -- your SO post was the clue :) I haven't seen ".provider" before so thanks for that also, as well as the hint about injection into the $get instead. |
Awesome, cheers guys! 👍 |
Hey I had the same problem and I solved like this... a mix of the stuff before... 'use strict'; angular.module('srcApp', [...])
}) |
So I've got some route handling code like:
Only problem is that the resolve function breaks everything when minified; is there a way around this?
The text was updated successfully, but these errors were encountered: