根据条件更改多个按钮类

时间:2014-01-24 10:28:17

标签: javascript angularjs twitter-bootstrap angularjs-ng-click ng-class

我正在构建一个多选题问题网络应用。每个问题都有5个选项。在单击按钮之前,它具有默认按钮类:btn btn-default。目前我已经做到了这一点,当做出选择时,所有按钮都被取消激活,并且会出现一个div,其中对答案的解释是填充的。

如果选择了正确的答案,我想将按钮类更改为btn btn-success,但如果选择了错误的答案,我想将错误的选择更改为btn btn-danger,并将{0}的正确答案更改为btn btn-warning 1}}。

这是我的控制器:

    .controller('ChoiceCtrl',['$scope', '$http', function($scope, $http){
    $scope.choiceb = true;
    $scope.isDisabled = false;
    $scope.disableClick = function() {
        $scope.isDisabled = true;
        return false;
    }
}])

这是我的HTML:

    <div ng-controller="ChoiceCtrl">
    <div id="choices">

        <h3>Choose your answer:</h3>            
        <button class="btn btn-default choice" ng-click="myApp.choiceb=!myApp.choiceb; disableClick()" ng-disabled="isDisabled" ng-model="isDisabled">Choices#01</button><br>
        <button class="btn btn-default choice" ng-click="myApp.choiceb=!myApp.choiceb; disableClick()" ng-disabled="isDisabled" ng-model="isDisabled">Choices#02</button><br>
        <button class="btn btn-default choice" ng-click="myApp.choiceb=!myApp.choiceb; disableClick()" ng-disabled="isDisabled" ng-model="isDisabled">Choices#03</button><br>
        <button class="btn btn-default choice" ng-click="myApp.choiceb=!myApp.choiceb; disableClick()" ng-disabled="isDisabled" ng-model="isDisabled">Choices#04</button><br>
        <button class="btn btn-default choice" ng-click="myApp.choiceb=!myApp.choiceb; disableClick()" ng-disabled="isDisabled" ng-model="isDisabled">Choices#05</button>

    </div>

    <div id="answer" class="choiceb" ng-if="myApp.choiceb"></div>

    <hr>

    <div id="rating">
        <h4>Rate this question:</h4>
        <h4 id="thumbs">
            <a href="#">
                <i class="glyphicon glyphicon-thumbs-up" id="voteup"></i>
            </a>
            <a href="#">
                <i class="glyphicon glyphicon-thumbs-down" id="votedown"></i>
            </a>
        </h4>
    </div>

    <button class="btn btn-primary pull-right choiceb" id="next-question" ng-if="myApp.choiceb">Next Question</button>

</div>

我已经能够使用ng-class将正确的答案更改为绿色,但是在以橙色突出显示正确答案的同时无法将错误答案变为红色。任何帮助都会很棒。谢谢!

0 个答案:

没有答案