2

I'm trying to toggle a class when a button is clicked. IE: if button 'rndTotal' is selected, class "selected" is added, and the other buttons have class "selected" removed. I also need this approach to work with my controller, as in, I will have stuff happen based on what is selected.

So far I have:

      <div class="row optionRow" ng-init="option = 'exact'" >
        <div class="col">
          <button ng-class="{selected:option=='exact'}">Exact</button>
        </div>
        <div class="col">
          <button ng-class="{selected:option=='rndTip'}">Round Tip</button>
        </div>
        <div class="col">
          <button ng-class="{selected:option=='rndTotal'}">Round Total</button>
        </div>
      </div>

It defaults to 'exact' but doesn't function other than that.

7
  • What is select($index) doing? Commented Jun 2, 2016 at 21:02
  • At the moment...nothing. I was trying to figure out what is the best method to then reference in my controller. Commented Jun 2, 2016 at 21:03
  • Not answering the question, but this directive does exactly what you need. (If you want to include another dependency in your project, off course). Commented Jun 2, 2016 at 21:04
  • you could try replacing select($index) with select('exact') for example, and then have $scope.select assign the argument ('exact') to the variable $scope.option. Commented Jun 2, 2016 at 21:06
  • Possible duplicate of AngularJS toggle class using ng-class Commented Jun 2, 2016 at 21:07

1 Answer 1

1

This feels like a hack, but to accomplish what you're after you could do this:

<div class="row optionRow" ng-init="option = 'exact'" >
    <div class="col">
        <button ng-class="{selected:option=='exact'}" ng-click="option='exact'">Exact</button>
    </div>
    <div class="col">
        <button ng-class="{selected:option=='rndTip'}" ng-click="option='rndTip'">Round Tip</button>
    </div>
    <div class="col">
        <button ng-class="{selected:option=='rndTotal'}" ng-click="option='rndTotal'">Round Total</button>
    </div>
</div>

Update

Instead of setting model values directly in the markup like this, I would move to using a controller. In order to do that I would make the following changes:

Controller

.controller('myController', function() {
    var vm = this;
    vm.selectedOption = 'exact';
    vm.selectOption = function(option) {
        vm.selectedOption = option;
        // do anything else you may need to do when the selected option changes
    };
});

Markup:

<div ng-controller="myController as vm">
    <div class="row optionRow">
        <div class="col">
            <button ng-class="{selected:vm.selectedOption==='exact'}" ng-click="vm.selectOption('exact')">Exact</button>
        </div>
        <div class="col">
            <button ng-class="{selected:vm.selectedOption==='rndTip'}" ng-click="vm.selectOption('rndTip')">Round Tip</button>
        </div>
        <div class="col">
            <button ng-class="{selected:vm.selectedOption==='rndTotal'}" ng-click="vm.selectOption('rndTotal')">Round Total</button>
        </div>
    </div>
</div>
Sign up to request clarification or add additional context in comments.

4 Comments

This seems to work perfectly. Could you ELI5 why you (1) think it's hacky, and (2) why this solution even works? I am pretty new to Angular so I'm trying to figure out the best way to do things.
I don't like to set model values directly in the markup like this. I prefer to use the controller for things like this. I'll update my answer with how I would approach this.
This is perfect. Thank you for the explanation and providing your preferred method.
@nc45 I just realized that I didn't explain why the first answer works. When you use ng-init="option = 'exact'" you are implicitly creating a model property named option. I took advantage of this and just directly set the value of that model property in the ng-click event of each button. Hopefully that makes sense.

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.