0

我在ng-click中有一个变量设置为 true,但下面的 div 没有显示。我已经关注了这篇文章,但它似乎在ng-repeat中不起作用?这是 plunker:http ://plnkr.co/edit/90G1KAx9Fmf2SgRs5gYK?p=preview

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

2 回答 2

1

它应该是ng-click="$parent.flag = true;reach(111);"

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

外部 ng 重复

您的问题不清楚,您可以通过在父范围内ng-repeat维护变量来使用 ng-repeat inside 。并使用内部注释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>

在 ng-repeat 里面

于 2015-04-01T16:12:16.257 回答
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>

于 2015-04-01T16:21:41.783 回答