點(diǎn)擊ng-repeat遍歷后的li標(biāo)簽晰绎,實(shí)現(xiàn)更改border顏色赋兵;
html代碼:
<ul ng-if="params.questionTypeId == 8">
<li class="gamePre-def" ng-repeat=" word in wordsDef" ng-click='li_click($index)'
ng-class='{focus: $index == focus}'> {{word}}</li>
</ul>
js代碼:
$scope.li_click = function (i) {
$scope.focus = i;
};
說(shuō)明:
①在< li >元素的單擊事件中铲咨,將執(zhí)行$scope對(duì)象中添加的"li_click()"方法;
②在該方法中將"$index(行號(hào)值)"作為實(shí)參傳給方法嘲更,并將"focus"屬性值設(shè)為“$index”值闸溃;
③因此當(dāng)單擊某行< li >元素時(shí),"focus"屬性值將變?yōu)橄鄳?yīng)的"$index"勋陪;
④此時(shí),< li >元素的"ng-class"樣式指令通過(guò)key/value對(duì)象的方式指定該元素需要添加的樣式,由于單擊< li >元素時(shí)贪磺,"$index"變量值和"focus"屬性值相同,也就是說(shuō)"$index==focus"的返回值為true诅愚;
⑤此時(shí)您應(yīng)該明白了缘挽,"ng-class"的樣式指令值變?yōu)?focus";
⑥經(jīng)過(guò)上面的分析及操作,我們實(shí)現(xiàn)了單擊< li >元素時(shí),添加border的效果.
css樣式:
ul .focus {
border: 1px solid blue;
}