angularjs通过指令传递索引

问题描述 投票:-1回答:2

我需要与此代码有关的帮助。

<ul ng-init="round = 'round'">
  <li ng-repeat="mesa in mesas" ng-click="selected($index)">
    <div id="{{round}}">&nbsp;</div>
    MESA {{mesa}}
   </li>
 </ul>


$scope.selected = function ($index){
    $scope.index.round = 'round1';
  }

我只需要单击要更改css名称的li,但是它会更改我列出的所有li。

angularjs angularjs-scope
2个回答
1
投票

0
投票
<html ng-app="app"> <head> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular.min.js"></script> <style type="text/css"> .round { background: green; } .round1 { background: blue; } </style> </head> <body ng-controller="controller"> <ul> <li ng-repeat="mesa in mesas" ng-click="selected($index)" ng-class="[index[$index].round]"> <div id="{{ index[$index].id }}">&nbsp;</div> MESA {{ mesa }} </li> </ul> </html> <script type="text/javascript"> angular.module('app', []) .controller('controller', controller); controller.$inject = ['$scope']; function controller($scope) { $scope.mesas = ['1', '2', '3']; $scope.index = [{ id: '1', round: 'round' }, { id: '2', round: 'round' }, { id: '3', round: 'round' }]; $scope.selected = function($index) { $scope.index[$index].round = $scope.index[$index].round === 'round' ? 'round1' : 'round'; } } </script>
© www.soinside.com 2019 - 2024. All rights reserved.