2015-04-01 35 views
0

我在ng-click中設置了一個變量true,但是下面的div沒有顯示。我遵循this post,但它看起來不起作用,也許ng-repeat?這裏的plunker:http://plnkr.co/edit/90G1KAx9Fmf2SgRs5gYK?p=previewangularjs-ng-show不能與ng-repeat配合使用

angular.module('myAppApp', []) 

    .controller('MainCtrl', function ($scope) { 
     $scope.notes = [{ 
      id: 1, 
      label: 'First Note', 
      done: false, 
      someRandom: 31431 
     }, { 
      id: 2, 
      label: 'Second Note', 
      done: false 
     }, { 
      id: 3, 
      label: 'Finished Third Note', 
      done: true 
     }]; 



     $scope.reach= function (id) { 
      //the assignment below works 
      //$scope.flag = true; 
      alert("hello there"); 
     }; 


}); 



<div ng-app="myAppApp"> 
    <div ng-controller="MainCtrl"> 
     <div ng-repeat="note in notes"> 
     {{note.id}} - {{note.label}} - 
     <a href="#" ng-click="flag = true;reach(111);">click me</a> 

     </div> 

     <div class="row" id="" ng-show="flag">I'm Here</div> 
    </div> 
    </div> 

回答

0

我會建議你使用ng-init

<div ng-repeat="note in notes" ng-init="parent=$parent"> 

,之後

<a href="#" ng-click="parent.flag = true;reach(111);">click me</a> 

請參閱下面的演示

angular.module('myAppApp', []) 
 

 
.controller('MainCtrl', function($scope) { 
 
    $scope.notes = [{ 
 
    id: 1, 
 
    label: 'First Note', 
 
    done: false, 
 
    someRandom: 31431 
 
    }, { 
 
    id: 2, 
 
    label: 'Second Note', 
 
    done: false 
 
    }, { 
 
    id: 3, 
 
    label: 'Finished Third Note', 
 
    done: true 
 
    }]; 
 

 

 

 
    $scope.reach = function(id) { 
 
    //$scope.flag = true; 
 
    alert("hello there"); 
 
    }; 
 

 

 
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 

 
<body> 
 
    <div ng-app="myAppApp"> 
 
    <div ng-controller="MainCtrl"> 
 
     <div ng-repeat="note in notes" ng-init="parent=$parent"> 
 
     {{note.id}} - {{note.label}} - 
 
     <a href="#" ng-click="parent.flag = true;reach(111);">click me</a> 
 

 
     </div> 
 

 
     <div class="row" id="" ng-show="flag">I'm Here</div> 
 
    </div> 
 
    </div> 
 
</body>

1

應該ng-click="$parent.flag = true;reach(111);"

<a href="#" ng-click="flag = true;reach(111);">click me</a> 

Outside ng-repeat

您的問題不清楚,你可以使用NG重複內ng-repeat,在ng-repeat父範圍保持變量。並使用訪問父範圍$parent.註釋裏面ng-repeat

<div ng-repeat="note in notes"> 
    {{note.id}} - {{note.label}} - 
    <a href="#" ng-click="$parent.selected = note.id;reach(111);">click me</a> 
    <div class="row" id="" ng-show="$parent.selected == note.id">I'm Here</div> 
    </div> 
</div> 

Inside ng-repeat