Reputation: 5409
I know that ng-view
is better for this type of problem, but I'm already using it to dynamically load an auth
vs. dashboard
template, so I'm stuck using ng-includes
.
Basically, once I load the dashboard
template with ng-view
, I have a tab control that should change the ng-include
depending on which tab is active. However, I can not for my life figure out how to change the ng-include depending on which tab is selected. As an FYI, super new to Angular, mostly a JQuery guy, (and yes, I know I should avoid using JQuery w/ Angular so that I can master Angular, but I'm at my wits end with this thing).
Here's my tab control:
myApp.controller('TabsCtrl', ['$scope', function ($scope) {
$scope.tabs = [{
url: '#/dashboard/one'
}, {
url: '#/dashboard/two'
}
}];
$scope.activeTab = '#/dashboard/one';
$scope.onClickTab = function(tab) {
$scope.activeTab = tab.url;
}
$scope.isActiveTab = function(tabUrl) {
return tabUrl == $scope.activeTab;
}
$scope.dashboard.url = {};
if($scope.activeTab === '#/dashboard/one') {
$('ng-include').attr('src', 'dashboard/one.html');
}
if($scope.activeTab === '#/dashboard/two') {
$('ng-include').attr('src', 'dashboard/two.html');
}
}]);
And here's the html:
<div id="wrapper" ng-controller="TabsCtrl">
<div id="sidebar-wrapper">
<ul class="sidebar-nav">
<li ng-repeat="tab in tabs"
ng-class="{active_tab:isActiveTab(tab.url)}"
ng-click="onClickTab(tab)">
<a href="{{tab.url}}">
<div class="tab-icon">
<i ng-class="tab.icon"></i>
</div>
<div class="tab-label">{{tab.label}}</div>
</a>
</li>
</ul>
</div>
<div id="page-content-wrapper">
<div class="page-content">
<ng-include src=""></ng-include>
</div>
</div>
</div>
@hasH: I tried to do something along these lines, but $scope.dashboardPath.url
didn't seem to be the actual src="dashboardPath.url"
.
var path = currentPath;//Retrieve it.
$scope.dashboardPath={};
if(path==='/dashboard/one')
$scope.dashboardPath.url='pageOne.html';
if(path==='/dashboard/two')
$scope.dashboardPath.url='pageTwo.html';
Upvotes: 2
Views: 16805
Reputation: 52847
Bind the src attribute of the ng-include directive with an angular expression that evalutes to activeTab.url
. activeTab
is a model on the scope, and src
will be bound to the url
property of the model:
<div id="page-content-wrapper">
<div class="page-content">
<ng-include src="activeTab.url"></ng-include>
</div>
</div>
In your controller, make sure you assign activeTab
to your scope:
myApp.controller('TabsCtrl', ['$scope', function ($scope) {
$scope.tabs = [{
url: '#/dashboard/one'
}, {
url: '#/dashboard/two'
}
}];
$scope.activeTab = $scope.tabs[0];
$scope.onClickTab = function(tab) {
$scope.activeTab = tab;
}
$scope.isActiveTab = function(tabUrl) {
return tabUrl == $scope.activeTab.url;
}
}]);
There is no need to manipulate the DOM with ng-include (it would be incorrect to do so). When 'activeTab' changes, it will automatically update ng-include because there is a binding set up between the $scope model (activeTab) and the ng-include directive.
Upvotes: 10
Reputation: 3749
First of all, been there. Here is what I had done.
var controller = function($scope, $location)
{
$scope.dashboard.url = {};
$scope.init = function(){
var currentLink = $location.url();
if(currentLink === '#/dashboard/one') {
$scope.dashboard.url = 'dashboard/one.html';
}
if(currentLink === '#/dashboard/two') {
$scope.dashboard.url = 'dashboard/two.html';
}
};
$scope.init();
};
[EDIT] Maybe this could work also.
var controller = function($scope, $location)
{
$scope.tabs = [{url: '#/dashboard/one'},
{url: '#/dashboard/two'}
];
$scope.dashboard={};
$scope.dashboard={url:$scope.tabs[0].url};
$scope.init = function(){
var currentLink = $location.url();
if(currentLink === '#/dashboard/one') {
$scope.dashboard = $scope.tabs[0];
}
if(currentLink === '#/dashboard/two') {
$scope.dashboard.url = $scope.tabs[0];
}
};
$scope.init();
};
Upvotes: 1
Reputation: 101
As the source to the ng-include you need to bind a variable from the scope.
myApp.controller('TabsCtrl', ['$scope', function ($scope) {
$scope.tabs = [{
url: '#/dashboard/one'
}, {
url: '#/dashboard/two'
}
}];
$scope.activeTab = '#/dashboard/one';
$scope.onClickTab = function(tab) {
$scope.activeTab = tab.url;
$acope.pageSource = tab.url;
}
$scope.isActiveTab = function(tabUrl) {
return tabUrl == $scope.activeTab;
}
$scope.dashboard.url = {};
if($scope.activeTab === '#/dashboard/one') {
$scope.pageSource = 'dashboard/one.html';
}
if($scope.activeTab === '#/dashboard/two') {
$scope.pageSource = 'dashboard/two.html';
}
}]);
<div id="wrapper" ng-controller="TabsCtrl">
<div id="sidebar-wrapper">
<ul class="sidebar-nav">
<li ng-repeat="tab in tabs"
ng-class="{active_tab:isActiveTab(tab.url)}"
ng-click="onClickTab(tab)">
<a href="{{tab.url}}">
<div class="tab-icon">
<i ng-class="tab.icon"></i>
</div>
<div class="tab-label">{{tab.label}}</div>
</a>
</li>
</ul>
</div>
<div id="page-content-wrapper">
<div class="page-content">
<ng-include src="pageSource"></ng-include>
</div>
</div>
</div>
Upvotes: 2