如何在不使用角度为ng-class的情况下动态地应用ng-style?类似于仅为所选活动菜单更改颜色。像这样的DEMO,但有ng风格。下面是我的代码,它是切换功能。任何人都可以通过更改代码或使用您自己的示例来解决此问题,以便在单击时将活动项目的颜色或字体大小更改,并将其余项目更改为默认状态。
<div ng-style="x.selected && {'color':'white','font-weight':'bold'}"
ng-click="select(x)" ng-repeat="x in myData" ></div>
var app = angular.module('anApp', ['angular.filter']);
app.controller('aCtrl', function($scope) {
$scope.data = [
{
"id": 100,
"value": "2452"
},
{
"id": 100,
"value": "2458"
},
{
"id": 1,
"value": "2457"
},
{
"id": 1,
"value": "2459"
},
{
"id": 4,
"value": "2460"
},
{
"id": 5,
"value": "3458"
}
];
$scope.select = function(x){
x.selected = !x.selected;
}
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/angular-filter/0.4.7/angular-filter.js"></script>
<div ng-app="anApp" ng-controller="aCtrl">
<div ng-style="x.selected && {'color':'red','font-weight':'bold'}"
ng-click="select(x)" ng-repeat="x in data" >
{{x.value}}
</div>
</div>
答案 0 :(得分:1)
试试这样。
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {
$scope.menuItems = ['Home', 'Contact', 'About', 'Other'];
$scope.activeMenu = $scope.menuItems[0];
$scope.setActive = function(menuItem) {
$scope.activeMenu = menuItem
}
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/angular-filter/0.4.7/angular-filter.js"></script>
<div ng-app="plunker" ng-controller="MainCtrl">
<div class="account-item" ng-repeat='item in menuItems'>
<div class="account-heading" ng-style="activeMenu === item && {'background' :'red' }">
<h4 class="account-title">
<a href="#/Messages" ng-click="setActive(item)"> {{ item }}</a>
</h4>
</div>
</div>
</div>
&#13;
答案 1 :(得分:1)
你可以尝试使用$ index,如下面的plunker,
http://plnkr.co/edit/Z977olOlajTNZENbqx7D?p=preview
<div ng-repeat="x in data" ng-click="setSelected($index)" ng-style="$index === selectedId && {'background' :'red' }">
{{x.value}}
</div>