Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
83
rated 0 times [  87] [ 4]  / answers: 1 / hits: 16857  / 10 Years ago, tue, june 10, 2014, 12:00:00

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';

More From » jquery

 Answers
59

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.


[#70636] Sunday, June 8, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
tyreese

Total Points: 739
Total Questions: 95
Total Answers: 98

Location: Belize
Member since Mon, Jun 20, 2022
2 Years ago
;