admin管理员组文章数量:1289957
Hi I am trying to make certain sections open (i.e. 'call.html' not to perform authentication) for my single page app in angular.js using login service.
I would like to know how can I get path() so I can pare it and redirect to 'login' page if the page is not 'call.html' ?
I have got the following code in my app.js which seems to work but doesn't work in Chrome and I get following error:
TypeError: $location.path(...).contains is not a function
my app.js.
app.run(['$rootScope', '$location', '$cookieStore', '$http',
function ($rootScope, $location, $cookieStore, $http)
{
$rootScope.globals = $cookieStore.get('globals') || {};
if ($rootScope.globals.currentUser)
{
$http.defaults.headersmon['Authorization'] = 'Basic' + $rootScope.globals.currentUser.authdata; // jshint ignore:line
}
$rootScope.$on('$locationChangeStart', function (event, next, current)
{
// redirect to login page if not logged in
if($location.path().contains('call'))
{
return;
}
else
{
if ($location.path() !== '/login' && !$rootScope.globals.currentUser && $location.path() !=='/')
{
$location.path('/login');
}
}
});
}
]);
Any help of how to address this would be greatly appreciated.
Hi I am trying to make certain sections open (i.e. 'call.html' not to perform authentication) for my single page app in angular.js using login service.
I would like to know how can I get path(http://www.example./app/#/call/1234) so I can pare it and redirect to 'login' page if the page is not 'call.html' ?
I have got the following code in my app.js which seems to work but doesn't work in Chrome and I get following error:
TypeError: $location.path(...).contains is not a function
my app.js.
app.run(['$rootScope', '$location', '$cookieStore', '$http',
function ($rootScope, $location, $cookieStore, $http)
{
$rootScope.globals = $cookieStore.get('globals') || {};
if ($rootScope.globals.currentUser)
{
$http.defaults.headers.mon['Authorization'] = 'Basic' + $rootScope.globals.currentUser.authdata; // jshint ignore:line
}
$rootScope.$on('$locationChangeStart', function (event, next, current)
{
// redirect to login page if not logged in
if($location.path().contains('call'))
{
return;
}
else
{
if ($location.path() !== '/login' && !$rootScope.globals.currentUser && $location.path() !=='/')
{
$location.path('/login');
}
}
});
}
]);
Any help of how to address this would be greatly appreciated.
Share Improve this question edited Dec 22, 2015 at 4:15 Jia Jian Goi 1,4233 gold badges21 silver badges33 bronze badges asked Dec 22, 2015 at 3:57 snowflakes74snowflakes74 1,3071 gold badge20 silver badges48 bronze badges 1- 1 I would remend using ngRoute docs.angularjs/api/ngRoute or ui-router github./angular-ui/ui-router – Shaun Scovil Commented Dec 22, 2015 at 4:01
3 Answers
Reset to default 8contains is a jquery method not of angularjs. So, using this line of code:
if($location.path().contains('call')){
will obviously throws an error, "contains is not a function".
You can use indexOf:
if($location.path().indexOf('call') > -1){
The $locationProvider
uses hashbangs by default (as evident from your path /app/#/call/1234
) so if you don't set html5Mode
to true
, you will get an empty string if you try to fetch the URL path.
Try setting $locationProvider.html5Mode(true)
and you should be able to get the URL path.
You can do :-
var url = $location.url();
if(url.indexOf('call') > -1){
console.log("Call present");
}
本文标签: javascriptCompare URL in Angular using locationpath()Stack Overflow
版权声明:本文标题:javascript - Compare URL in Angular using $location.path() - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741420041a2377747.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论