SentinelSixSpy SentinelSixSpy - 3 years ago 74
HTML Question

Use one md-switch to toggle a group of other md-switchs in HTML/JS

I am wanting to be able to toggle and untoggle entire group sections. When the group section is toggle off, I want the sections in the group toggle off as well or disabled. If the group section is toggled on, I want the sections toggled on as well or enabled. I added

to the group toggle with the function
. I also have a
inside the function that goes through the feeds in the group. I am just not sure what to add to turn off or on the feeds in the group.

Answer Source

I finally got this to work properly. These are the changes I made.


        <md-switch class="md-primary" ng-model="layerGroup.state" ng-change="toggleLayerGroup(layerGroup)" aria-label="{{layerGroup.displayName}} Toggle Switch"></md-switch>

      <div  ng-repeat="feed in layerGroup.feeds" layout="row" layout-align="start center" style="padding: 0 16px;">
        <!--FEED NAME-->
        <p flex class="m2" style="color: #FFFFFF;" ng-style="{ 'opacity' : layerGroup.state ? '1' : '.3' }">{{}}</p>
        <!--FEED SWITCH-->
        <md-switch class="md-primary" ng-model="feed.layerState" ng-disabled="!layerGroup.state" ng-change="toggleLayer(feed)" aria-label="{{}} Toggle Switch"></md-switch>

I added ng-style to show when the feeds are disabled. And ng-disabled to disable the switch when the layerGroupState is false.


// -- load feeds
  if (EcosystemService.currentUserOrg.feeds) {
    var feeds = EcosystemService.currentUserOrg.feeds;
    angular.forEach(feeds, function(feed, index) {
      var lg = $scope.layerGroups[feed.category];
      if(lg) {
        lg.feeds[] = feed;
        lg.feeds[].layerState = EcosystemService.currentUser.mapState.visibleLayers.indexOf( >= 0;
        if (lg.feeds[].layerState) {
          lg.state = true;

$scope.toggleLayerGroup = function (layerGroup) {
  if (layerGroup.state) {
    angular.forEach(layerGroup.feeds, function (feed) {

      if (EcosystemService.currentUser.mapState.visibleLayers.indexOf( < 0) {
      feed.layerState = true;
  else {
    angular.forEach(layerGroup.feeds, function (feed) {

      var ndex = EcosystemService.currentUser.mapState.visibleLayers.indexOf(;
      if (ndex >= 0) {
        EcosystemService.currentUser.mapState.visibleLayers.splice(ndex, 1);
      feed.layerState = false;

I added code to the place where the feeds are loaded and added code in toggleLayerGroup that will change the layerState dependending on the value of the layerGroup.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download