I am new to AngularJS and I've been unable to find specific tutorials of a list and grid view toggle switch buttons that loads in two different HTML partials. Read official ng-include
, ng-switch
official docs and searched SO. Unfortunately, we don't want to use UI-router.
Is loading in two partials (list.html
and grid.html
) the correct Angular way of coding this?
The most relevant help I've found are these:
1.http://tutorialzine.com/2013/08/learn-angularjs-5-examples (Example #5)
There was an insightful comment on Example #5
Nice simple examples - well done. The last example that switches between grid and list views is not very efficient since it creates both options and the shows/hides one. A simpler/better approach would be using a single ul with repeater and ng-switch and then enabling the alternate list elements using ng-switch-case. - Johan
2.http://www.adobe.com/devnet/html5/articles/getting-started-with-angularjs.html
3.create a single html view for multiple partial views in angularjs
4.Conditional ng-include in angularjs
My HTML code
<div class="col-xs-6" ng-controller="ToggleDisplayCtrl">
<div class="btn-group select-format-container" ng-switch on="selected">
<button ng-switch-when="true" ng-click="toggleGrid()" type="button" class="btn btn-primary" ng-model="formatChoice" ng-disabled="">grid</button>
<button ng-switch-when="false" ng-click="toggleList()" type="button" class="btn btn-primary" ng-model="formatChoice" ng-disabled="">list</button>
</div>
<div ng-include src="formatChoice.url" scope="" onload=""></div>
</div><!-- col-xs-6 END ToggleDisplayCtrl-->
My directive code
'use strict';
var app = angular.module('tempApp');
app.controller('ToggleDisplayCtrl', function($scope) {
$scope.formatChoices =
[ { name: 'grid', url: 'partials/grid.html'},
{ name: 'list', url: 'partials/list.html'} ];
$scope.selected = true;
$scope.toggleGrid = function() {
if(selected) {
return "partials/grid.html";
}
return "main.html";
};
$scope.toggleList = function() {
if(selected) {
return "partials/list.html";
}
return "main.html";
};
});
Here's a simple working JSBin: http://jsbin.com/jomiquqi/1/edit
The
ng-include
directive takes a variable for the template to include. So to update your code to get it working, yourtoggleGrid()
method shouldn't be returning something, it should be setting a variable on the scope which you pass tong-include
like in my example.For better understanding of toggling action to see the list and grid view: follow this link and I hope this is helpful.
In this we used Jquery, if you are experimenting on this. Do place this:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
The easiest way to do this is:
CSS:
Basically what you need to do is put a list view and grid same time on the same page and display one at a time, toggling between them(changing css class) with the switch button.I'll discuss the Example #5 here:
First layout variable in the scope
Here are the switch buttons
These are the grid and list blocks.
Css that makes the list and grid layout
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
Main page of application (index.html)
Route template(main.html)
List view template (list.html)
Grid view template (grid.html)
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
andlist.html
are very similar, so if you have only these two options you may decide not to useng-include
with separate switchable views at all, but place content view directly into your route's template and just switch classes used in panels usingng-class
directive which can switch classes when view is changed.Route template(main.html)