2016-08-30 188 views
1

我遇到了麻煩,在這裏爲我們的應用程序的特定要求。角度ui路由器定製問題

我們正在一個預先存在的Rails應用程序中設置一個角度應用程序。

由於我們代碼庫的完整重構目前是不合理的,我們正在處理一些硬性定製和雙方的黑客,以允許Angular的增量引入。

我們現在需要的是一種告訴ui路由器只綁定到我們擁有ng-sref屬性的鏈接並且不用打擾我們應用中的所有常規href鏈接的方法。

有沒有辦法實現這種行爲?

下面是我當前的代碼:


angular.module('app').config(function($stateProvider, $urlRouterProvider, $locationProvider) { 
    $stateProvider.state('test', { 
        url: "/1/test", 
        template: 'test' 
       }); 

      $locationProvider.html5Mode({ 
       enabled: true, 
       requireBase: false, 
       rewriteLinks: false 
      }) 
     } 
    ) 

採用這種方法,所有的鏈接,即使是那些沒有任何路線設置它,並沒有一個用戶界面,SREF屬性,正在被角路由服務,並觀看阻止它像之前的行爲一樣工作。我不想將我們巨大的應用程序的每條路線添加到角度路由設置(這是一個可怕的想法),因爲大多數這些鏈接是非角度頁面。我只想讓角度路由服務忽略這個鏈接,或者這些未定義的位置。也許爲$ location服務設置讓它們與之前的行爲一起休眠(ajax請求,常規請求或Turbolinks請求)。我很確定這是一件非常微不足道的事情,我可能會忽略這一點。

當我點擊這些鏈接時,會發生什麼情況,窗口位置會發生變化,但什麼也沒有發生。瀏覽器請求未被觸發。

非常感謝您提前。

+1

如果ui路由器有一個匹配href的url,它會去那裏。如果您沒有匹配的網址(或沒有匹配的網址),那麼它只會匹配在那裏設置的網址。 –

+0

謝謝!但這不適合我。也許是因爲我正在使用html5mode。我已經在que問題中添加了當前的代碼。 –

回答

0
################################################################################### 
# My suggestion is use the ui-router to route to specific pages as shown 
# in the example below. 

# <a ui-sref="/login"> 
# <a ui-sref="/logout"> 
# <a ui-sref="/signup"> 
# <a ui-sref="/profile"> 

# 'ui-sref' will take you to the specific pages. 

# You can also use '$state.go' to take you to specific pages as shown below. 

# $state.go('authentication.login'); 
# $state.go('authentication.logout'); 
# $state.go('authentication.signup'); 
# $state.go('authentication.profile'); 

################################################################################### 

(function() { 
    'use strict'; 

    angular 
     .module('app.foo.authentication') 
     .config(moduleConfig); 

    /* @ngInject */ 
    function moduleConfig($translatePartialLoaderProvider, $stateProvider) { 
     $translatePartialLoaderProvider.addPart('app/foo/authentication'); 

     $stateProvider 
     .state('authentication.login', { 
      url: '/login', 
      templateUrl: 'app/foo/authentication/login/login.tmpl.html', 
      controller: 'LoginController', 
      controllerAs: 'vm' 
     }) 
     .state('authentication.logout', { 
      url: '/logout', 
      templateUrl: 'app/foo/authentication/logout/logout.tmpl.html', 
      controller: 'LogoutController', 
      controllerAs: 'vm' 
     }) 
     .state('authentication.signup', { 
      url: '/signup', 
      templateUrl: 'app/foo/authentication/signup/signup.tmpl.html', 
      controller: 'SignupController', 
      controllerAs: 'vm' 
     }) 
     .state('authentication.profile', { 
      url: '/profile', 
      templateUrl: 'app/foo/authentication/profile/profile.tmpl.html', 
      controller: 'ProfileController', 
      controllerAs: 'vm' 
     }); 
    } 
})(); 
+0

我相信我的問題可能是啓用了html5選項。這個簡單的基本方法不適用於我。我在上面添加了我的當前代碼。 –

+0

謝謝。但這對我不起作用。我已經爲我的問題添加了更多解釋。 –