【问题标题】:Uncaught SyntaxError: Unexpected token . in angular $locationUncaught SyntaxError: Unexpected token 。在角度$位置
【发布时间】:2014-07-17 03:35:25
【问题描述】:

$location.path('/user/logout');

此行在控制台中收到错误Uncaught SyntaxError: Unexpected token .。当该行被评论时,它就会发生。请问我的代码有什么问题。

angular.module('symphony', ['ngRoute'])

        .config(function($routeProvider, $locationProvider, $location) {

    $routeProvider
            .when('/user', {
        templateUrl: '/angular/pages/user.php',
        controller: enterpriseController,
    })

            .when('/modules', {
        templateUrl: '/angular/pages/merchant/dash.php',
        controller: merchantController,
    })

             .when('/user/logout', {
        $location.path('/user/logout');
    });


    $locationProvider.html5Mode(true);
});

【问题讨论】:

    标签: angularjs


    【解决方案1】:

    问题是由以下代码引发的:

    .when('/user/logout', {
        $location.path('/user/logout');
    });
    

    函数when的第二个参数是一个对象,所以不能把$location.path('/user/logout');放在这里!

    你路由/user/logout的目的是什么?

    ======================================

    如果你需要服务器端来处理路由,试试这个:

    .when('/user/logout', {
        template: '<div></div>',
        controller: function(){
            window.location.href= "/user/logout";
        }
    });
    

    【讨论】:

    • 因为我所有的链接都是有角度的路线。所以页面不会重新加载。但我想强制 /user/logout 在没有角度路由的情况下重新加载。
    • @Namal 我不明白你为什么需要这样做,你是说这个吗? .when('/user/logout', {redirectTo: '/user/logout'})这肯定会导致死循环!
    • 那你能告诉我如何在没有ajax的角度路由下发送服务器请求吗?因为我当前的所有请求都是基于 ajax 的,并且正常的页面请求都被 angular 禁用了。
    • 你的意思是当用户路由到'/user/logout'时,向服务器'/user/logout'发送一个ajax请求?
    • 你的意思是你需要服务器端来处理'/user/logout',而不是角度,对吧?而你的服务器端有模板来负责这个请求?
    猜你喜欢
    • 2012-05-17
    • 2019-02-10
    • 2014-07-08
    • 2011-03-09
    • 2014-01-06
    • 2012-04-10
    • 1970-01-01
    相关资源
    最近更新 更多