【问题标题】:$routeProvider not redirecting [duplicate]$routeProvider 不重定向 [重复]
【发布时间】:2017-07-03 18:19:22
【问题描述】:

我有这样的 config()。

function config($httpProvider, $routeProvider) {

$httpProvider.interceptors.push('httpInterceptorService');

$routeProvider
  .when('/detail/:id', {
    controller: 'CourseDetailController',
    controllerAs: 'vm',
    templateUrl: 'templates/course-detail.html'
  })
.otherwise({
    redirectTo: '/'
  });
}

查看

<a class="course--module course--link" href="/#/detail/{{ course._id   }}">

当点击课程时,URL 编码如下,似乎没有发生路由。

http://localhost:5000/#!/#%2Fdetail%2F57029ed4795118be119cc43d

我试过了

  1. href = "/#/detail/{{ course._id }}" 中删除了/#,但这次URL 变成了http://localhost:5000/detail/57029ed4795118be119cc43d,而不是拾取角度路由器并发出服务器请求。

  2. 手动访问地址栏URL http://localhost:5000/#!/detail/F57029ed4795118be119cc43d 没有编码字符,这一次视图被正确更改。

有什么想法吗?

【问题讨论】:

    标签: angularjs


    【解决方案1】:

    在您的配置中执行此操作,

    function config($httpProvider, $routeProvider, $locationProvider) {
        $locationProvider.hashPrefix('');
    

    并尝试使用 ng-href

    <a class="course--module course--link" ng-href="/#/detail/{{ course._id   }}">
    

    只需在 href 中的 # 之后添加 !

    <a class="course--module course--link" ng-href="/#!/detail/{{ course._id   }}">
    

    【讨论】:

    • 似乎不起作用
    • 你使用的是 angular 1.6 吗?
    【解决方案2】:

    您需要使用浏览器级别的 API、对象窗口,或者您可以在控制器中注入 $window 引用。

    • window.location.href="http://www.google.com";
    • $window.location.href="http://www.google.com";

    【讨论】:

    • 如果我解决了您的问题,请接受答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-19
    • 2020-01-09
    • 2014-09-24
    • 2018-02-16
    • 1970-01-01
    相关资源
    最近更新 更多