12

I have a button, clicking on it should show/hide some area.

button(ng-click="areaStatus='on'")
.area(ng-class="areaStatus")

I want not to just use ng-show/ng-hide and then assign it to Boolean areaStatus, but I want more complex things like on/off/hidden/transparent/whatever.

Is there a way to toggle areaStatus between 'on' and 'off' on click without having to write a function for it, just with an inline expression?

1
  • can't you use ng-switch in that case? Commented Aug 28, 2013 at 20:23

3 Answers 3

18

You can do this (HTML):

<button ng-click="value = { 'on': 'off', 'off':'on'}[value]">On/Off</button>

jsFiddle

But it's very ugly. I would definitely create a method on the scope to change the state, especially if it's more complicated than just toggling two values.

However, if areaStatus is just to change the area class, I think you should drop it, and instead change the class accordingly to your model state. Something like this:

function Ctrl($scope) {
    $scope.state = 'on';

    $scope.changeState = function() {
        $scope.state = $scope.state === 'on' ? 'off' : 'on';
    }
}

...

<area ng-class="{'on': state, 'off': !state }">

I've used 'on' and 'off', but it should be values that make sense to your model.

Sign up to request clarification or add additional context in comments.

1 Comment

I was stumped when the ternary operator $scope.state = 'on' ? 'off' : 'on'; wasn't working. I was missing the additional $scope.state ===. Thank you!
12

It's not too clear what you're wanting or why you are avoiding wanting a function, but a way that I'd go about doing what I think you want to do:

<button ng-click="areaStatus = !areaStatus">Toggle</button>
<div ng-class="{'red' : areaStatus, 'green' : !areaStatus}">
    This is some text
</div>

Here is a fiddle that shows it as well as well as a code snippet below.

var myModule = angular.module('myModule', []);

myModule.controller('myController', function ($scope) {

    $scope.areaStatus = false;
});
.red {
    color:red;
}
.green {
    color:green;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.3/angular.min.js"></script>
<div ng-app="myModule">
    <div ng-controller="myController">
        <button ng-click="areaStatus = !areaStatus">Toggle</button>
        <div ng-class="{'red' : areaStatus, 'green' : !areaStatus}">
            This is some text
        </div>
    </div>
</div>

Within the ng-click you just simply flip the boolean value of areaStatus. With ng-class you can pass in an object. The red and green would be the classes that you want to apply. They will be applied when the expression following is correct.

An alternative would be to use Angular's {{ }} within your class:

<div class="{{areaStatus}}">   </div>

So if areaStatus contains the string "hidden", then that is what class will be in there. But you'd still need a way to then change the value of areaStatus (either in a function, multiple buttons, or checkboxes).

Comments

0

I think inline change is a good idea:

(ngModelChange)="data.is_toggole=(data.is_toggole== 0 ? 0 : 1)"

data.is_toggole //Your variable

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.