我是angularJs的新手,我的角度控制器中有一个函数
$scope.addPrimaryClass = function () {
var weekDayBtn = $(".weekdayButton");
weekDayBtn.addClass("btn-white").removeClass("btn-primary");
$.each(weekDayBtn,function (index) {
if($(this).val()==$scope.weekly){
$(this).addClass("btn-primary").removeClass("btn-white");
}
})
};
在视图中我有:
<div class="btn-group">
<button class="btn btn-white weekdayButton" type="button" value="7" ng-click="weekdayClicked($event);">Sun</button>
<button class="btn btn-white weekdayButton" type="button" value="1" ng-click="weekdayClicked($event);">Mon</button>
<button class="btn btn-primary weekdayButton" type="button" value="2" ng-click="weekdayClicked($event);">Tue</button>
<button class="btn btn-white weekdayButton" type="button" value="3" ng-click="weekdayClicked($event);">Wed</button>
<button class="btn btn-white weekdayButton" type="button" value="4" ng-click="weekdayClicked($event);">Thu</button>
<button class="btn btn-white weekdayButton" type="button" value="5" ng-click="weekdayClicked($event);">Fri</button>
<button class="btn btn-white weekdayButton" type="button" value="6" ng-click="weekdayClicked($event);">Sat</button>
</div>
addPrimaryClass 中的代码在控制台中按要求工作,因为我可以在那里使用普通的jquery,但代码本身不起作用。基本上,我想根据范围值之一更改按钮的CSS。请帮忙!!!
答案 0 :(得分:1)
您可以使用ng-class
指令根据范围变量的内容应用类。例如:
<button class="btn weekdayButton" type="button" value="7" ng-click="weekdayClicked($event);" ng-class="($(this).val()==$scope.weekly) ? 'btn-primary' : 'btn-white'">Sun</button>
使用ng-class
指令有几种方法。请查看文档并确定最适合您的内容:https://docs.angularjs.org/api/ng/directive/ngClass
另外,您可能希望从Angular代码中删除所有jQuery:)