[英]How to enable lazy loading on tabs in tabset in Angular js?
I making <tabset>
view to show the content in a page. 我制作
<tabset>
视图以在页面中显示内容。 Also I am using Bootstrap's Tabs and Lazy Data Loading so that the controller for a particular tab is initialized only when it is active. 另外,我正在使用Bootstrap的选项卡和惰性数据加载,以便仅在活动选项卡时才初始化特定选项卡的控制器。 I am able to achieve lazy loading for the tabs.
我可以实现选项卡的延迟加载。
<div id="panel" ng-controller="HomeController as hmctrl">
<tabset>
<tab class="nav-item" id="tab_content" title="Tab1" template-url="app/components/tab1/tab1.html"></tab>
<tab class="nav-item" id="tab_content" title="Tab2" template-url="app/components/tab2/tab2.html"></tab>
<tab class="nav-item" id="tab_content" title="Tab3" template-url="app/components/tab3/tab3.html"></tab>
</tabset>
</div>
In my app.config I have only one route for the main page 在我的app.config中,主页只有一条路线
var app = angular.module('myApp', ['bootstrap.tabset','ngRoute']);
app.config(
function($routeProvider,$locationProvider) {
$routeProvider
.when('/', {
templateUrl: 'app/components/home/homeView.html'
});
});
For every tab there is a separate controller assigned to them. 对于每个选项卡,都有一个单独的控制器分配给它们。 I would like to know how to assign routes to any particular tabs when created this way.
我想知道以这种方式创建时如何将路由分配给任何特定的选项卡。 So that I can navigate to them using
$location.path()
function. 这样我就可以使用
$location.path()
函数导航到它们。
I have created a plnkr for the same here Plnkr for the above described scenario 我为上述情况在这里创建了一个plnkr
You should be using $stateProvider instead of $routeProvider in this case. 在这种情况下,应使用$ stateProvider而不是$ routeProvider。 I have forked your plunker here- https://plnkr.co/edit/lL9JVRmuE8kQ5f0WhRDo?p=preview
我在这里分叉了你的朋克-https://plnkr.co/edit/lL9JVRmuE8kQ5f0WhRDo ? p = preview
var app = angular.module('myApp', ['bootstrap.tabset','ngRoute','ui.router']);
app.config(
function($stateProvider, $urlRouterProvider) {
$urlRouterProvider.otherwise("/Home");
$stateProvider
.state('Home', {
url: "/Home",
templateUrl: "home.html"
})
.state('Home.Tab1', {
url: "/Tab1",
templateUrl: "tab1.html"
})
.state('Home.Tab2', {
url: "/Tab2",
templateUrl: "tab2.html"
})
.state('Home.Tab3', {
url: "/Tab3",
templateUrl: "tab3.html"
});
});
Index.html - use ui-view instead of ng-view Index.html-使用ui-view而不是ng-view
<div class="ui-view"></div>
Home.html Home.html
<div id="panel" ng-controller="HomeController as hmctrl">
<p>Home</p>
<tabset>
<tab class="nav-item" id="tab_content" title="Tab1" ui-sref=".Tab1"></tab>
<tab class="nav-item" id="tab_content" title="Tab2" ui-sref=".Tab2"></tab>
<tab class="nav-item" id="tab_content" title="Tab3" ui-sref=".Tab3"></tab>
</tabset>
<div ui-view></div>
</div>
The url will be #/Home/Tab1 ,#/Home/Tab2, #/Home/Tab3 and you could force navigate to the state by: 网址为#/ Home / Tab1,#/ Home / Tab2,#/ Home / Tab3,您可以通过以下方式强制导航到状态:
$state.go("Home.Tab2");//navigates to Tab2 of Home page
More about state providers here - https://github.com/angular-ui/ui-router 有关状态提供程序的更多信息-https: //github.com/angular-ui/ui-router
Make sure you refer state provider script and inject this module in your app 确保引用状态提供程序脚本并将此模块注入您的应用程序
You could add separate routes to each tabs, 您可以为每个标签添加单独的路线,
app.config(
function($routeProvider,$locationProvider) {
$routeProvider
.when('/', {
templateUrl: 'app/components/home/homeView.html'
controller:'HomeCtrl'
});
.when('/Tab1', {
templateUrl: 'app/components/tab1/Tab1.html',
controller:'Tab1Ctrl'
});
})
You can use $routeParams
service. 您可以使用
$routeParams
服务。 Take a look at the example code below. 看下面的示例代码。
Change your route configuration to include a tab identifier. 更改您的路由配置以包括标签标识符。
app.config(
function($routeProvider,$locationProvider) {
$routeProvider
.when('/:tabId', {
templateUrl: 'app/components/home/homeView.html',
controller:'HomeController'
});
});
Then in your HomeController
you can access the $routeParams
service to get the current value of tabId
. 然后在你
HomeController
您可以访问$routeParams
服务获取的当前值tabId
。
app.controller('HomeController', function($scope, $routeParams) {
$scope.activeTab = $routeParams.tabId;
});
Set the active property of tab accordingly. 相应地设置选项卡的active属性。
<tab active="{{activeTab == 1}}" class="nav-item" id="tab_content" title="Tab1" template-url="app/components/tab1/tab1.html" active="{{activeTab == 1}}"></tab>
<tab active="{{activeTab == 2}}" class="nav-item" id="tab_content" title="Tab2" template-url="app/components/tab2/tab2.html"></tab>
<tab active="{{activeTab == 3}}" class="nav-item" id="tab_content" title="Tab3" template-url="app/components/tab3/tab3.html"></tab>
Then you can call your tabs as <baseurl>/1
, <baseurl>/2
然后,您可以将标签称为
<baseurl>/1
, <baseurl>/2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.