Serhiy Serhiy - 2 months ago 8
AngularJS Question

How to pass data to other scopes variables in angularjs?

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

Answer

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;
            });
        }
    }
}());