使用$ location.path()比较Angular中的URL

sno*_*s74 3 javascript angularjs

嗨我正在尝试使用登录服务为angular.js中的单页应用程序打开某些部分(即'call.html'不执行身份验证).

我想知道如何获取路径(http://www.example.com/app/#/call/1234),以便我可以比较它并重定向到"登录"页面,如果该页面不是'call.html '?

我的app.js中有以下代码似乎有效,但在Chrome中不起作用,我收到以下错误:

TypeError: $location.path(...).contains is not a function
Run Code Online (Sandbox Code Playgroud)

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.common['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');
                }
            }
        });
    }
]);
Run Code Online (Sandbox Code Playgroud)

如何解决这个问题的任何帮助将不胜感激.

Bho*_*yar 8

contains是一个不是angularjs的jquery方法.所以,使用这行代码:

if($location.path().contains('call')){
Run Code Online (Sandbox Code Playgroud)

显然会抛出一个错误,"包含不是一个功能".

你可以使用indexOf:

if($location.path().indexOf('call') > -1){
Run Code Online (Sandbox Code Playgroud)