如何将数据传递给angularjs中的其他范围变量?

时间:2022-03-27 14:18:58

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

任务是当我点击一个它应该设置所有vm.show = false

2 个解决方案

#1


1  

TRY THIS ONE: HTML:

尝试这一个: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;
            });
        }
    }
}());

#2


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

#1


1  

TRY THIS ONE: HTML:

尝试这一个: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;
            });
        }
    }
}());

#2


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