2017-02-16 7 views
1

私はこのngRepeatを持っています。ユーザーがボタンをクリックしたときに入力が表示されるように、ある種のトグルを作成したいと思いますが、表示されている他のすべての入力を非表示にします(すでに表示されている場合)。ng-repeatで角度表示を非表示にする

<ul> 
    <li ng-repeat="(key, value) in jewel"> 
     <span ng-show="!showMe">text here</span> 
     <input ng-show="showMe"> 
     <button ng-click="showMe = true"></button> 
    </li> 
</ul> 

希望します。

+0

あなたはNG-ショーを=しようとした入力をしていますng- hide = "!showMe" –

答えて

0

ng-repeatは、スコープを分離しましたそのため、親スコープから変数を使用して可視性を追跡する必要がある他のすべてのインプットに影響を与えるのです。私は例とフィドルをprepearedました:http://jsfiddle.net/ancvgc1b/

angular 
    .module('myApp', ['ui.bootstrap']) 
    .controller('ExampleController', function($scope){ 
     $scope.jewel = { 
     key1: 'Foo', 
     key2: 'Bar' 
     }; 
     $scope.visible = { key: 'key1' }; 

     $scope.setVisible = function(key) { 
     $scope.visible.key = key; 
     } 
    }); 

<body ng-app="myApp"> 
    <div ng-controller='ExampleController'> 
    <ul> 
     <li ng-repeat="(key, value) in jewel"> 
     <span ng-show="!(visible.key == key)">Text value</span> 
     <input ng-show="visible.key == key"> 
     <button ng-click="setVisible(key)">show input</button> 
     </li> 
    </ul> 
</div> 
</body> 
+0

ng-repeatはスコープを分離するのではなく、子スコープを作成します。 ソリューションは問題ありませんが、あまり冗長にすることはできません。 – pranavjindal999

0

クイックフィックス:

<ul ng-init="active={}"> 
    <li ng-repeat="(key, value) in jewel"> 
     <span ng-show="!($index==active.index)">text here</span> 
     <input ng-show="$index==active.index"> 
     <button ng-click="active.index = $index"></button> 
    </li> 
</ul> 

あなたがcontrollerAsを使用している場合は、次のように、それは非常にクリーンなコードにつながる: (VMがコントローラにあなたのエイリアスです)

<ul> 
    <li ng-repeat="(key, value) in jewel"> 
     <span ng-show="!($index==vm.activeIndex)">text here</span> 
     <input ng-show="$index==vm.activeIndex"> 
     <button ng-click="vm.activeIndex = $index"></button> 
    </li> 
</ul> 
0

はこれを試してみてください: "!SHOWME"

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

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

 
$scope.people = [ 
 
    { 
 
    id: 1, 
 
    name: "Alpha", 
 
    age: "24", 
 
    clicked : false 
 
    }, 
 
{ 
 
    id: 2, 
 
    name: "Beta", 
 
    age: "25", 
 
    clicked : false 
 
    } 
 
]; 
 

 
$scope.showInput = function(person,index) { 
 
person.input = true; 
 
$scope.index = index; 
 
}; 
 

 
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<div ng-app="myApp" ng-controller="MyCtrl"> 
 
    <ul> 
 
    <li ng-repeat="person in people"> 
 
     <span ng-show="(!person.input && (index != $index))">text here</span> 
 
     <input ng-show="(person.input && (index == $index))"> 
 
     <button ng-click="showInput(person,$index)">Show Input</button> 
 
    </li> 
 
    </ul>  
 
</div>

関連する問題