angular.js - 如何在ng-repeat时默认第一个元素background-color为红色,点击对应元素背景变红同时更换其他全为黑色
过去多啦不再A梦
过去多啦不再A梦 2017-05-15 17:04:32
0
2
578

就是在ng-click中动态更改样式,请问有什么思路或方法嘛?急急急!!在线等!!!拜托了各位大神!!

过去多啦不再A梦
过去多啦不再A梦

Antworte allen(2)
给我你的怀抱

提供一个简单思路:
JS:


$scope.isActive = 0;
$scope.arr = [
    {
        //code here
    },
    {
        //code here
    }
];
$scope.selectItem = function (index) {
    item.isActive = index;
}

HTML:

<ul>
    <li ng-repeat="item in arr track by $index" ng-class="{red:isActive === $index, black:!isActive === $index}" ng-click="selectItem($index)"></li>
</ul>

CSS:

.red {
    background: red;
}
.black {
    background: black;
}

修改你的代码就是这样:

HTML:



<p class="p10 c-white w50p fl mr1 tc" ng-repeat="e in item.images">
    <p class="w50p h50 img_p" ng-class="{b-red:isActive === $index,b-white:!isActive === $index}" ng-click="changeGood($index)">
        <img src="{{e.path}}">
    </p>
    <span class="ib w50p fs0-6 span-over">{{e.description}}</span>
</p>

controller:


$scope.isActive=0;

$scope.changeGood=function(index){
    $scope.isActive=index;
    
}
Ty80

我之前回答过另一个人提出的和你类似的问题,我直接把之前写的demo改一改发给你了,完全符合你的要求,你预览下,如果看不到效果就要翻下墙,demo如下:
Demo

Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage