You should define application controller's scope property that will hold URL of template for ng-include
, and bind this property to your directive's scope. Make sure to use isolated scope in your directive in order to avoid side effects while modifying directive's scope. Here is an example of doing it (see comments in the code):
JavaScript
angular.module('app', ['ngRoute'])
.config(['$routeProvider', function($routeProvider) {
$routeProvider
.when('/main', {
controller: 'appController',
templateUrl: 'main.html'
})
.otherwise({
redirectTo: '/main'
});
}])
.controller('appController', ['$scope', function($scope) {
$scope.view = 'list.html'; // <- default template used for ng-include
$scope.data = [{
text: '1'
}, {
text: '2'
}, {
text: '3'
}, {
text: '4'
}, {
text: '5'
}, {
text: '6'
}];
}])
.directive('appView', function() {
return {
scope: {
// link view property of directive's scope to some property
// in the parent scope (scope of appController) specified in
// app-view attribute of root element of directive
view: '=appView'
},
replace: true,
template:
'<nav class="navbar navbar-default">' +
'<div class="container">' +
'<ul class="nav navbar-nav navbar-right">' +
'<li ng-repeat="v in views" ng-bind="v.name" ' +
'ng-class="v.icon" ng-click="switchView(v)"></li>' +
'</ul>' +
'</div>' +
'</nav>',
link: function(scope, el, attr) {
scope.views = [{
name: 'List',
template: 'list.html',
icon: 'btn btn-default navbar-btn glyphicon glyphicon-th-list'
}, {
name: 'Grid',
template: 'grid.html',
icon: 'btn btn-default navbar-btn glyphicon glyphicon-th'
}];
},
controller: ['$scope', function($scope) {
$scope.switchView = function(view) {
$scope.view = view.template; // <- modify parent scope view
}
}]
}
});
Main page of application (index.html)
<html ng-app="app">
...
<body ng-view=""></body>
</html>
Route template (main.html)
<header app-view="view"></header>
<section ng-include="view"></section>
List view template (list.html)
<div class="container">
<div class="row">
<div class="col-md-12 col-sm-12 panel panel-default" ng-repeat="item in data">
<div class="panel-body">{{item.text}}</div>
</div>
</div>
</div>
Grid view template (grid.html)
<div class="container">
<div class="row">
<div class="col-md-4 col-sm-6 panel panel-default" ng-repeat="item in data">
<div class="panel-body">{{item.text}}</div>
</div>
</div>
</div>
Plunker: http://plnkr.co/edit/uWw7NuPG0I161mHXZg2r?p=preview
Bonus: grid is responsive, just play a bit with a window size
Another option:
As you have probably noticed, grid.html
and list.html
are very similar, so if you have only these two options you may decide not to use ng-include
with separate switchable views at all, but place content view directly into your route's template and just switch classes used in panels using ng-class
directive which can switch classes when view is changed.
Route template (main.html)
<header app-view="view"></header>
<section>
<div class="container">
<div class="row">
<div ng-class="{'col-md-4': view === 'grid.html',
'col-md-12': view === 'list.html',
'panel':true, 'panel-default':true}"
ng-repeat="item in data">
<div class="panel-body">{{item.text}}</div>
</div>
</div>
</div>
</section>