ngClass style with dash in key

后端 未结 3 623
陌清茗
陌清茗 2020-12-12 21:40

I hope this saves someone a headache with styles that use dashes, especially since bootstrap has become so popular.

I am using angular 1.0.5 by way of



        
相关标签:
3条回答
  • 2020-12-12 21:50

    \'icon-white\' works as well (with AngularJS 1.2.7)

    0 讨论(0)
  • 2020-12-12 21:53

    After hours of hacking around, it turns out the dash gets interpolated! Quotes are needed.

    <i class="icon-home" ng-class="{'icon-white': someBooleanValue}">
    

    I hope this helps someone from tearing their hair out.

    UPDATE:

    In older versions of Angular, using a backslash also does the trick, but not in the newer versions.

    <i class="icon-home" ng-class="{icon\-white: someBooleanValue}">
    

    The former is probably preferred, since you can more easily search for it in your favorite editor.

    0 讨论(0)
  • 2020-12-12 21:58

    alternative for uses ng-class :

        .menu-disabled-true{
    color: red;
    }
        .menu-disabled-false{
    color: green;
    }
    
    
    <div ng-controller="DeathrayMenuController">
    <p class=menu-disabled-{{status}}>shanam</p>
     <button ng-click="action()">click me</button>
    </div>
    
    <script>
    
    
    
    function DeathrayMenuController($scope) {
        $scope.status=true
        $scope.action= function(){
          $scope.status=!$scope.status
        }
    }
    </script>
    
    0 讨论(0)
提交回复
热议问题