ng-show ng-hide in ng-repeat如何为每个包装块单击/触发

时间:2016-04-15 03:36:42

标签: javascript angularjs

如何点击隐藏/显示包含在li中的div /类的div,现在如果我点击第一个项目,则两个项目都将显示。有没有像jQuery这样的方法来检查Angular中的this

在线示例:http://jsfiddle.net/qp0x1zcc/

 <div ng-app="editer" ng-controller="myCtrl" class="container">
  <ul ng-repeat="item in items">
    <li ng-click="show = !show" ng-init='show = false'>
      <span ng-hide="show">{{item.name}}</span>
      <form ng-show="show">
        <input ng-model="item.name">
      </form>
    </li>
    <li ng-click="show = !show">
      <span ng-hide="show">{{item.d}}</span>
      <form ng-show="show">
        <input ng-model="item.d">
      </form>
    </li>
  </ul>
</div>

1 个答案:

答案 0 :(得分:1)

试试这样。简单明了。

&#13;
&#13;
var editer = angular.module('editer', []);
function myCtrl($scope) {
$scope.index = -1;
  $scope.index2 = -1;
  $scope.items = [{
    name: "item #1",
    d: "names 1"
  }, {
    name: "item #2",
    d: "names 2"
  }, {
    name: "item #3",
    d: "names 3"
  }];
  $scope.setIndex = function(item){
    $scope.index = $scope.items.indexOf(item);
     $scope.index2 = -1;
  
  }
    $scope.setIndex2 = function(item){
       $scope.index = -1;
     $scope.index2 = $scope.items.indexOf(item);
  
  }
  
  $scope.clearIndex = function(){
    $scope.index = -1;
     $scope.index2 = -1;
    }
  
}
&#13;
.container {
  margin-top: 10px;
  font-family: arial;
}

.container header {
  padding-bottom: 20px;
  border-bottom: 1px solid black;
}

ul,
input,
.container {
  padding: 10px;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="editer" ng-controller="myCtrl" class="container">


  <ul ng-repeat="item in items">

    <li ng-click="setIndex(item)"  ng-dblClick = "clearIndex()">
      <span ng-show="index != $index">{{item.name}}</span>
      <form ng-show="index == $index">
        <input ng-model="item.name">
      </form>
    </li>
     <li ng-click="setIndex2(item)"  ng-dblClick = "clearIndex()">
           <span ng-show="index2 != $index">{{item.d}}</span>
      <form ng-show="index2 == $index">
        <input ng-model="item.d">
      </form>
    </li>
  </ul>
</div>
&#13;
&#13;
&#13;