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.https://mcmap.net/q/112725/-create-a-single-html-view-for-multiple-partial-views-in-angularjs/12584774#12584774
4.https://mcmap.net/q/222737/-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 JS 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";
};
});
<button>
instead. – Beefy