【问题标题】:Angular app always redirects to defaultAngular 应用程序总是重定向到默认值
【发布时间】:2015-06-11 03:14:22
【问题描述】:

我正在构建一个新的 Rails API / Angular 应用程序。我有两个基本页面,一个是 Gulp 生成的默认页面。我对这些重定向有点迷茫。

如果我输入这个:

http://localhost:9000/main

浏览器将主页重定向到这个地址(注意哈希):

http://localhost:9000/#/main

当我输入时:

http://localhost:9000/sentences

然后浏览器也会重定向主页,并在浏览器中使用此地址:

http://localhost:9000/sentences#/main

'use strict';

angular.module('myapp', ['ngAnimate', 'ngCookies', 'ngTouch', 'ngSanitize', 'ngRoute', 'ui.bootstrap'])
  .config(function ($routeProvider) {
    $routeProvider
      .when('/main', {
        templateUrl: 'app/main/main.html',
        controller: 'MainCtrl'
      })
      .when('/sentences', {
         templateUrl: 'app/sentences/sentences.html',
         controller: 'SentencesCtrl'
      })
     .otherwise({
        redirectTo: '/main'
      });  
  })
;

【问题讨论】:

    标签: ruby-on-rails angularjs gulp route-provider


    【解决方案1】:

    添加 $locationProvider 并将 html5Mode 设置为 true 可以,但不确定原因。

    'use strict';
    
    angular.module('myapp', ['ngAnimate', 'ngCookies', 'ngTouch', 'ngSanitize', 'ngRoute', 'ui.bootstrap'])
      .config(function ($routeProvider,$locationProvider) {
         $locationProvider.html5Mode(true);
        $routeProvider
          .when('/main', {
            templateUrl: 'app/main/main.html',
            controller: 'MainCtrl'
          })
          .when('/sentences', {
             templateUrl: 'app/sentences/sentences.html',
             controller: 'SentencesCtrl'
          })
         .otherwise({
            redirectTo: '/main'
          });  
      }) 
    ;
    

    我还需要在我的 index.html 中添加一个基本标签

        <base href="/">
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-02
    • 2016-10-10
    • 2018-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多