Javascript 如何根据有角度的路线给div一个类?
我有一个简单的角度模板,它做一个简单的ng重复,包含一些div。现在,如果Javascript 如何根据有角度的路线给div一个类?,javascript,html,css,angularjs,templates,Javascript,Html,Css,Angularjs,Templates,我有一个简单的角度模板,它做一个简单的ng重复,包含一些div。现在,如果$routeParams.userId等于循环中对象的id,我想给一个包含div的类。请参阅下面代码的第二行以了解我的意思 <div class="row" ng-repeat="contact in contacts"> <div class="col-xs-12 contact-wrapper {{ if ($routeParams.userId == contact.id){ print '
$routeParams.userId
等于循环中对象的id,我想给一个包含div的类。请参阅下面代码的第二行以了解我的意思
<div class="row" ng-repeat="contact in contacts">
<div class="col-xs-12 contact-wrapper {{ if ($routeParams.userId == contact.id){ print 'active' } }}"> <!-- <== SOMETHING LIKE THIS -->
{{ contact.user_name }}
<br />{{ contact.last_pm_text }}
</div>
</div>
{{contact.user_name}
{{contact.last\u pm\u text}
有人知道当
contact.id
等于$routeParams.userId
时,我如何给div设置类active
?欢迎所有提示 您可以使用ng类
<div class="col-xs-12 contact-wrapper" ng-class="{'active' : (userIdx == contact.id)}">
ngClass指令允许您通过数据绑定表示要添加的所有类的表达式,在HTML元素上动态设置CSS类
您只需要使用ng类:
{{contact.user_name}
{{contact.last\u pm\u text}
$scope.userIdx = $routeParams.userId;
<div class="col-xs-12 contact-wrapper" ng-class="{'active' : $routeParams.userId == contact.id}">
{{ contact.user_name }}
<br />{{ contact.last_pm_text }}
</div>