2016-05-06 41 views
0

我正在使用角度包模板。這裏11頁加載和11個不同的按鈕,如下所示:在AngularJS刷新或重新加載頁面時templateUrl發生變化

enter image description here

這裏是路線代碼:

state('app.pagelayouts.fixedsidebar1', { 
    url: "/fixed-sidebar", 
    templateUrl: "assets/views/page-1.html", 
    resolve: loadSequence('d3', 'ui.knob', 'countTo', 'dashboardCtrl'), 
    title: 'Fixed Sidebar', 
    ncyBreadcrumb: { 
     label: 'Fixed Sidebar' 
    }, 
    controller: function ($scope) { 
    $scope.setLayout(); 
    $scope.app.layout.isSidebarFixed = true; 

    $scope.$on('$routeChangeSuccess',function(){ 
     $scope.templateUrl = $route.current.templateUrl; 
    }) 
    } 
}) 
.state('app.pagelayouts.fixedsidebar2', { 
    url: "/fixed-sidebar", 
    templateUrl: "assets/views/page-2.html", 
    resolve: loadSequence('d3', 'ui.knob', 'countTo', 'dashboardCtrl'), 
    title: 'Fixed Sidebar', 
    ncyBreadcrumb: { 
    label: 'Fixed Sidebar' 
    }, 
    controller: function($scope) { 
    $scope.setLayout(); 
    $scope.app.layout.isSidebarFixed = true; 
    } 
}). 

頁3.html,頁面4.html等.. ..

假設我在頁面1.html,當我刷新它不留在頁面1。轉到另一頁。但它應該停留在page-1.html。 templateUrl正在改變。

我該如何解決?

+0

請出示其他國家的樣本。也許你對他們有相同的網址? –

+0

您可以[編輯]問題來添加更多信息,而不是評論。我已經爲你添加了問題。 –

+0

非常感謝T J。你給我提示。我更改了url url:「/ fixed-sidebar」,.Set url:「/ fixed-sidebar1」,url:「/ fixed-sidebar2」等等。 現在它的工作完美 – jonm

回答

0

一個顯而易見的問題是,對於所有路線,您都有相同的url。 您應該使用指示要加載的模板的參數對其進行更新,以便頁面刷新時,UI路由器根據url知道要激活哪個狀態。例如:

state('app.pagelayouts.fixedsidebar1', { 
    url: "/fixed-sidebar/1", 
    templateUrl: "assets/views/page-1.html", 
    resolve: loadSequence('d3', 'ui.knob', 'countTo', 'dashboardCtrl'), 
    title: 'Fixed Sidebar', 
    ncyBreadcrumb: { 
    label: 'Fixed Sidebar' 
    }, 
    controller: function($scope) { 
    $scope.setLayout(); 
    $scope.app.layout.isSidebarFixed = true; 
    } 
}); 

如果所有的路線都差不多,你可以創建一個像PARAM單一路線:

state('app.pagelayouts.fixedsidebar', { 
    url: "/fixed-sidebar/:id", 
    templateUrl: function($stateParams) { 
    return "assets/views/page-" + $stateParams.id + ".html"; 
    }, 
    resolve: loadSequence('d3', 'ui.knob', 'countTo', 'dashboardCtrl'), 
    title: 'Fixed Sidebar', 
    ncyBreadcrumb: { 
    label: 'Fixed Sidebar' 
    }, 
    controller: function($scope) { 
    $scope.setLayout(); 
    $scope.app.layout.isSidebarFixed = true; 
    } 
}); 
相關問題