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

时间:2016-09-14 10:49:03

标签: angularjs

我有控制器

play.http.errorHandler = "my.library.MyErrorHandler"
控制器中的

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

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

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

你必须要做

在你看来

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

在控制器功能中:

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