2016-03-18 39 views
0

我想從一個頁面導航到另一個頁面,並在另一個頁面上點擊按鈕時指定的元素。我怎樣才能做到這一點 ?導航到另一個頁面和特定元素Angularjs

我的場景:index.html和periodical.html是兩個文件,periodical.html有多個元素。現在,在index.html的點擊按鈕,我想直接導航至Periodical.html.I特定元素正在使用$stateProvider$urlRouterProvider

我的狀態(工作正常,直到導航到periodical.html):

.state('page.periodical', { 
       url: '/periodical', 
       controller: 'PeriodicalController', 
       controllerAs: 'periodical', 
       templateUrl: 'app/components/page/periodical/periodical.html', 
       data: { 
        pageTitle: 'Periodical page', 
        dashboard: 'periodical' 
       } 
     }) 

我正在從index.html控制器中做$state.go('page.periodical');,它工作正常。我怎樣才能達到$state.go('page.periodical#rt_page_element1');?這是導致錯誤,但元素參考#rt_page_element1是正確的。

+0

它不應該像 $ state.go('page.periodical #rt_page_element1' ); ? –

+0

對不起,這是一個錯字,編輯了這個問題 – Srini

回答

1

你必須來裝點$stateProvider

angular.module('app').config(['$provide', function ($provide) { 

    /** 
    * Extend the UI Router $stateProvider, adding the ability to specify an 
    * anchor hash as a parameter in the ui-sref directive. 
    */ 
    $provide.decorator('$state', ['$delegate', function ($stateProvider) { 

    // Save the orignal function for generating a state's URL. 
    var $stateHref = $stateProvider.href; 

    // Create our extended function. 
    $stateProvider.href = function href(stateOrName, params, options) { 
     var hash = ''; 
     params = params || {}; 
     var hashParam = params['#']; 

     // Check if the anchor parameter was specified. 
     if (typeof hashParam !== 'undefined') { 
     // Ensure hash parameter is a string and not empty. 
     if ((typeof hashParam === 'string' || hashParam instanceof String) && hashParam.length) { 
      hash = '#' + hashParam; 
     } 

     delete params['#']; 
     } 

     // Return the original parsed URL with the hash appended. 
     return $stateHref(stateOrName, params, options) + hash; 
    }; 

    return $stateProvider; 
    }]); 

}]); 

然後

<a ui-sref="page.periodical({'#': IDToScrollTo })"> 

的精確解discribed here

相關問題