Toggle css on ng-click
Asked Answered
U

2

9

This is the basic idea of my code:

HTML (jade):

#preferencesBox(ng-click="toggleCustom()")
   .glyphicon.glyphicon-heart

CSS:

#preferencesBox.active{
   color: #d04f37;
}

Angular:

$scope.check = true;
$scope.toggleCustom = function() {
    $scope.check = $scope.check === false ? true: false;
};

I want to add the css color : #d04f37 when the user clicks the parent #preferencesBox. Adding/removing .active is the jQuery way. How should my ng-class or the rest code look like?

Unwished answered 4/11, 2013 at 16:15 Comment(2)
So change color when the mouse is down and then back when released?Hescock
@oGeez No be permanent until the next click. Like a html checkbox.Unwished
D
16

You can use an expression inside ng-class that will watch the check variable:

ng-class="{'active' : check}"

When check = true, add class active

Dowel answered 4/11, 2013 at 16:28 Comment(0)
U
5

Take a look at the next example and apply in Jade:

<header ng-click="click()" ng-class="{'active': active == true}">Hello</header>

Then, in your controller:

$scope.click = function(){
    $scope.active = true;
}

I'd say this is simple enough to get you started and add logic for toggling into click() (it's only an if).

Upright answered 4/11, 2013 at 16:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.