Serhiy
Serhiy

Reputation: 1981

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

Upvotes: 0

Views: 40

Answers (2)

Michal Kucaj
Michal Kucaj

Reputation: 691

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

Upvotes: 1

jaguwalapratik
jaguwalapratik

Reputation: 406

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

Upvotes: 0

Related Questions