0

I have controller

<div ng-controller="LeftSideNavWorkController as vm"  ">
  <div ng-repeat="item in vm.items track by $index">
    <div ng-click="vm.hideAllElements()>Hide</div>
    <div ng-show = "showChildren[$index]" >Show/Hide element<div>
  </div>
</div>

in controller:

vm.hideAllElements = hideAllElements;
vm.items = [... ... ...]; //some array of items

function hideAllElements() {
//how set all showChildren[] variables to false?
 }

the task is that when I click on one it should set all vm.show = false

3
  • Close all repeated elements - not clear with this? What exactly is the desired result? Can you please elaborate? Commented Sep 14, 2016 at 10:53
  • edited. The final task is to close all opened elements when I click on any of <div>Hide</div> Commented Sep 14, 2016 at 10:57
  • You need to elaborate what is close and open with an example, otherwise, it is difficult to judge the desired functionality you are trying to achieve Commented Sep 14, 2016 at 11:05

2 Answers 2

1

TRY THIS ONE: HTML:

   <div ng-controller="LeftSideNavWorkController as vm"  ">
    <div ng-repeat="item in vm.items track by $index">
      <div ng-click="vm.hideAllElements()>Hide</div>
      <div ng-show = "showChildren[$index]" >Show/Hide element<div>
    </div>
   </div>

CTRL:

(function() {
    'use strict'
    angular
        .module('myApp')
        .controller('appController', appController);
    // main.js
    function appController($scope, $interval) {
        var vm = this;
        vm.items = [1, 2, 3, 4, 5];
        vm.hideAllElements = hideAllElements;
        vm.show = true;


        function hideAllElements() {
            vm.items.forEach(function(obj, i) {
                vm.show = false;
            });
        }
    }
}());
Sign up to request clarification or add additional context in comments.

1 Comment

where did you take app?
0

You just have to do

In your view

 <div ng-controller="LeftSideNavWorkController as vm"  ">
    <div ng-repeat="item in vm.items track by $index">
         <div ng-click="vm.hideAllElements()>Hide</div>
         <div ng-show = "item.show" >Show/Hide element<div>
    </div>
 </div>

In controller function:

function hideAllElements() {
    vm.items.forEach(function(item) {
       item.show = false;
    }
}

2 Comments

I've edited question. will not use vm.show any more .
@Serhiy as the controller level variable is shared inside view, you have to define property in each item to handle show/hide of item.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.