AngularJS $ routeProvider路由不正确 [英] AngularJS $routeProvider doesn't route properly

查看:47
本文介绍了AngularJS $ routeProvider路由不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我是Angular的新手,并试图弄清楚多条路线如何导致相同的view/templateUrl和控制器.这是我写的:

So I'm new to Angular and trying to figure out how multiple routes can lead to the same view/templateUrl and controller. Here is what I've written:

angular
  .module('mwsApp', [
    'ngAnimate',
    'ngCookies',
    'ngResource',
    'ngRoute',
    'ngSanitize',
    'ngTouch',
    'ui.bootstrap'
  ])
  .config(function ($routeProvider, $locationProvider) {
    // debugger
    $routeProvider
      .when('/', {
        templateUrl: 'index.html',
        controller: 'MainCtrl as vm',
      })
      .when('/rika', {
        templateUrl: 'index.html',
        controller: 'MainCtrl as vm',
      })
      // .otherwise({
      //   redirectTo: '/'
      // });

      $locationProvider.html5Mode(true).hashPrefix("!");
  });

问题:当前,如果我转到"localhost:9000/",则显示正确的版本,但是如果我转到"localhost:9000/rika",则显示无法获取/rika".

Issue: Currently, if I go to "localhost:9000/" it shows me the correct version, but if I go to "localhost:9000/rika" it gives me "Cannot get /rika".

调试:我已将问题缩小为html5Mode.我正试图摆脱#号!Angular会自动将其添加到URL上,但是当我这样做时,会弹出错误消息.

Debugging: I've narrowed down the problem to html5Mode. I'm trying to get rid of the #! that Angular adds automatically onto the URL, but when I do that, the error pops up.

感谢任何人的输入!

推荐答案

您无法在本地开发中摆脱#,因此您需要对此行添加注释

You cant get rid of # in local development so you need to comment this line

$locationProvider.html5Mode(true).hashPrefix("!");

但是,当您在生产中的某些服务器上时,则取消注释并使用它来删除#

But when you're on some server on production then uncomment and use it to remove the #

否则,在本地开发中的重新加载

otherwise you'll keep getting not get error on reloads in local development

这篇关于AngularJS $ routeProvider路由不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆