使用Ionic 框架,我试图创建一组三个按钮作为单选按钮:
如果我点击早餐,我希望午餐和晚餐恢复正常(白色)状态,早餐变成蓝色。
使用我当前的代码,我无法使用此功能,尽管我可以让按钮稍微随机地切换颜色(也许我只是不理解ng-class
指令)。
这是我的 HTML 代码:
<div class="bar bar-subheader">
<div class="button-bar">
<a class="button" ng-class="{'button-positive' : !isActiveB, 'none': isActiveB}" ng-click="active('breakfast')">Breakfast</a>
<a class="button" ng-class="{'button-positive' : !isActiveL, 'none': isActiveL}" ng-click="active('lunch')">Lunch</a>
<a class="button" ng-class="{'button-positive' : !isActiveD, 'none': isActiveD}" ng-click="active('dinner')">Dinner</a>
</div>
</div>
我的 JS:
$scope.active = function(meal) {
switch (meal) {
case 'breakfast':
$scope.$broadcast('slideBox.setSlide', 0);
$scope.isActiveB = $scope.isActiveB;
$scope.isActiveL = !$scope.isActiveL;
$scope.isActiveD = !$scope.isActiveD;
break;
case 'lunch':
$scope.$broadcast('slideBox.setSlide', 1);
$scope.isActiveB = !$scope.isActiveB;
$scope.isActiveL = $scope.isActiveL;
$scope.isActiveD = !$scope.isActiveD;
break;
case 'dinner':
$scope.$broadcast('slideBox.setSlide', 2);
$scope.isActiveB = !$scope.isActiveB;
$scope.isActiveL = !$scope.isActiveL;
$scope.isActiveD = $scope.isActiveD;
break;
}
};
如果您需要更多信息和可行的解决方案,我可以将代码放入 JSFidle。
谢谢你的帮助。
注意:我想维护我的active()
函数,并ng-class
尽可能使用该指令,因为我有很多其他代码依赖于这个函数。