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';
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';
Share
Improve this question
edited Jun 10, 2014 at 18:19
sir_thursday
asked Jun 10, 2014 at 18:08
sir_thursdaysir_thursday
5,41913 gold badges68 silver badges121 bronze badges
3
- So, you are not able to initialize properly I suppose ? – Gaurav Commented Jun 10, 2014 at 18:12
-
Hahaha, yeah... running into issues. Wele back to helping the Angular newbie. I couldn't initialize it properly, I think because
ng-include
creates a new scope, so I couldn't figure out where the code to initialize it should go. I tried putting it in theTabCtrl
, but couldn't figure out how to access thesrc
attribute. – sir_thursday Commented Jun 10, 2014 at 18:17 - 1 Let me try again. As for as fixing issues. That's what SO is for. – Gaurav Commented Jun 10, 2014 at 18:20
3 Answers
Reset to default 10Bind 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.
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>
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();
};