SELECT上的角度最接近的子项

时间:2017-02-28 22:35:00

标签: angularjs

Angular 1.4.x

ng-repeat我有一个SELECT控件,其下面有DIV个详细信息。当SELECT被更改时,我希望为DIV添加一个类。

BEGIN ng-repeat 
   <select ng-change="???add a class XYZ to .details ???">...</select>
   <div class="details">...</div>
END

1 个答案:

答案 0 :(得分:1)

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

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

  $scope.selection = [];
  $scope.isHighlighted = [];

  $scope.items = [{
    id: 1,
    label: 'Label 1',
  }, {
    id: 2,
    label: 'Label 2',
  }];

  $scope.selectionChanged = function(idx) {
    $scope.isHighlighted[idx] = true;
  }

});
.highlight {
  color: red;
}
<!DOCTYPE html>
<html ng-app="plunker">

<head>
  <meta charset="utf-8" />
  <script data-require="angular.js@1.5.x" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.10/angular.js" data-semver="1.5.10"></script>
</head>

<body ng-controller="MainCtrl">
  <div ng-repeat="item in items">
    <select ng-options="item as item.label for item in items track by item.id" ng-model="selection[$index]" ng-change="selectionChanged($index)"></select>
    <div ng-class="{highlight:isHighlighted[$index]}">Some Div Content</div>
  </div>
</body>

</html>