2013-05-28 14 views
6

$routeProvider$stateProviderを使用するAngularJSアプリがあります。 /から離れて私のルート/州をすべて稼働させることができます。ここでAngularJS:ルートでルートプロバイダが動作しない

は私app.jsは、私が/に行くが、私は/#templatesに行くときに、適切なビューとコントローラがで蹴るとき何も起こらない

var MailStash = angular.module("MailStash", ['ui.compat', 'ngResource', 'ngSanitize', 'ui.directives']). 
    config(function($stateProvider, $routeProvider, $urlRouterProvider) { 
     $routeProvider 
      .when('/', { controller: ListCtrl, templateUrl: '/js/partials/list.html' }); 

     $stateProvider 
      .state('templates', { 
        url: '/templates', 
        abstract: true, 
        templateUrl: '/js/partials/list.html', 
        controller: ListCtrl, 
      }) 
      .state('templates.list', { 
       // parent: 'templates', 
       url: '', 
       views: { 
       'main_content': { 
       templateUrl: '/js/partials/templates.new.html', 
       controller:CreateCtrl, 
       }, 
       } 
      }) 
      .state('templates.view', { 
       parent: 'templates', 
       url: '/{templateId}', 
       views: { 
       'main_content': { 
        templateUrl: '/js/partials/templates.view.html', 
        controller: ViewCtrl, 
       }, 
       }, 
      }) 
      .state('templates.new', { 
       url: '/new', 
       views: { 
       'main_content': { 
        templateUrl: '/js/partials/templates.new.html', 
        controller: CreateCtrl, 
       }, 
       }, 
      }) 
      .state('templates.edit', { 
       parent: 'templates', 
       url: '/edit/{templateId}', 
       views: { 
       'main_content': { 
        templateUrl: '/js/partials/templates.edit.html', 
        controller: EditCtrl, 
       }, 
       }, 
      }) 
    } 
    ) 
    .run(
     [  '$rootScope', '$state', '$stateParams', 
     function ($rootScope, $state, $stateParams) { 
      $rootScope.$state = $state; 
      $rootScope.$stateParams = $stateParams; 
     }] 
    ); 
... 
を提出。

誰が私と間違っているものを見ることができます$routeProvider、なぜ/に行かないのですか?

答えて

1

あなたは、これが役立ちます。この

$routeProvider 
     .when('/templates', { controller: ListCtrl, templateUrl: '/js/partials/list.html' }) 
     .otherwise({ redirectTo: '/templates' }); 

希望のようなデフォルトルートを指定することができます!

5

$urlRouterProviderを単に使用して/にルーティングするのはなぜですか?

$urlRouterProvider.otherwise('/'); 

は、次にそれ以外の場合は、URLが悪い場合は、エラーページにリダイレクトするために、より有用である /

$stateProvider.state({ 
    name: 'home', 
    url: '/', 
    controller: 'HomeCtrl', 
    templateURL: 'home.html' 
}); 
1

のための新しい状態を設定します。しかし、<base href="/" />を追加して、インデックスのベース位置を参照し、$ locationProvider.html5Mode(真)を追加することができます。あなたのconfig関数内のルートの宣言が有効になったら、それ以外の場合は#をURLに追加する必要があります。

私はこれがあなたに役立つことを願っています。

関連する問題