使用 angular 从内部循环中隐藏整个 div?

Using angular to hide a whole div from within an inner loop?

<div id="whole-div" ng-hide="hideme">
  <div id="loop-div" ng-repeat="i in [1, 2, 3]">
    <button ng-click="hideme=true">Button {{i}}</button> 
  </div>
</div>

以上是我现在的代码。我想要它,以便当您单击循环中的其中一个按钮(Button1、Button2、Button3)时,整个 div 被隐藏。 但是,我发现当按钮在外面时,我只能隐藏整个 div 如下...

<div id="whole-div" ng-hide="hideme">
  <div id="loop-div" ng-repeat="i in [1, 2, 3]">
    <button>Button {{i}}</button> </div>
  <button ng-click="hideme=true">Final Button</button>
</div>

有没有办法使用循环 div 中的一个内部按钮隐藏整个 div?提前致谢!

请检查工作示例:http://plnkr.co/edit/Itb2UG0fPFtqsdduOlHr?p=preview

HTML:

<div id="whole-div" ng-hide="hideme">
    <div id="loop-div" ng-repeat="i in [1, 2, 3]">
        <button ng-click="hideOuterDiv()">Button {{i}}</button>
    </div>
</div>

控制器:

$scope.hideOuterDiv = function() {
    $scope.hideme = true;
};

ng-repeat 创建一个局部范围,以便变量 hideme 属于该局部范围。实际上有 3 个变量 hideme,每个都在一个按钮的范围内。

在 $parent 作用域上设置 属性 应该有效,并且让 hideme 变量对于整个 div:

是唯一的

var app = angular.module('app', []);

app.controller('MainCtrl', function($scope) {

});
<!DOCTYPE html>
<html ng-app="app">

<head>
  <meta charset="utf-8" />
  <script src="https://code.angularjs.org/1.2.28/angular.js"></script>
</head>

<body ng-controller="MainCtrl">
  <!-- here is the scope of MainCtrl, hideme can be used as is -->
  <button ng-click="hideme=false">Show all</button>
  <div id="whole-div" ng-hide="hideme">
    <div id="loop-div" ng-repeat="i in [1, 2, 3]">
      <!-- here is the scope of a button, hideme have to be prefix by $parent to access the right property -->
      <button ng-click="$parent.hideme=true">Button {{i}}</button>
    </div>
  </div>
</body>

</html>

试试,

<div id="whole-div" ng-hide="hideme">
  <div id="loop-div" ng-repeat="i in [1, 2, 3]">
    <button ng-click="hideDivFunction();">Button {{i}}</button> 
  </div>
</div>

控制器,

$scope.hideme = false;
$scope.hideDivFunction= function(){
    $scope.hideme = !$scope.hideme; //in case toggle is required.
}

或者,

$scope.hideDivFunction= function(){
angular.element( document.querySelector('#whole-div')).toggle();
    }

我建议您更改 Controller 上的 $scope 值

<div id="whole-div" ng-hide="hideme" ng-controller="myCtrl">
  <div id="loop-div" ng-repeat="i in [1, 2, 3]" >
    <button ng-click="hide()">Button {{i}}</button> 
  </div>
</div>

在脚本中

app.controller('myCtrl',function($scope){
  $scope.hide = function(){$scope.hideme = true}
})