Change button color after click in Angularjs

前端 未结 3 1055
梦如初夏
梦如初夏 2021-01-20 16:39

This is what I have now: index.html

Todo

相关标签:
3条回答
  • 2021-01-20 17:17

    The probably best way is to bind the button style to the variable like this:

    <button class="button button-block {{buttonStyle}}" ng-click="start()">
        {{buttonText}}
    </button>
    

    and define buttonStyle and buttonText in your scope

    $scope.buttonText = "Start";
    $scope.buttonStyle="button-calm";
    
    $scope.start = function(){
        $scope.buttonText = "Clicked!";
        $scope.buttonStyle="button-assertive";
    }
    
    0 讨论(0)
  • 2021-01-20 17:22

    you can use ng-class Define a var like $scope.started = false; and inside you start function set it to true. On your button do this:

    ng-class="{'btn-warning': started, 'btn-danger': !started}"

    another way to write it:

    ng-class="started && 'btn-warning' || !started && 'btn-danger'"

    note: remove btn-danger from your curre class attribute

    EDIT

    The newer, more common way is the standard ternary operator (also easier to read):

    ng-class="started ? 'btn-warning' : 'btn-danger'"

    0 讨论(0)
  • 2021-01-20 17:27

    This can be done using directive easily. You can have a look at this. https://www.youtube.com/watch?v=sYexGR7FQX0

    0 讨论(0)
提交回复
热议问题