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
Add a ment  | 

3 Answers 3

Reset to default 8

contains 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