1

I am doing an SPA to test out my AngularJS knowledge. In this app, I have a template where-by users can choose buttons to show how many days of weather forecast they want.

I am using Twitter Bootstrap classes with the AngularJS ngClass to dynamically have the Twitter Bootstrap CSS classes bound to the button elements depending on which button is selected.

What I wish to happen is, with using ng-class in the HTML of the template, if the button is chosen, it will then change the Bootstrap CSS class of that button to btn btn-disabled from btn btn-primary, so it cannot be selected, as it all ready is selected. I did this in the HTML for this template as so —

  <div class="btn-group" role="group">
    <a href="#/forecast/1" class="btn btn-primary" ng-class="{  'btn btn-disabled' : days === '1' }">1 Day Forecast</a><a href="#/forecast/2" class="btn btn-primary" ng-class="{  'btn btn-disabled' : days === '2' }">2 Day Forecast</a><a href="#/forecast/3" class="btn btn-primary" ng-class="{  'btn btn-disabled' : days === '3' }">3 Day Forecast</a>
  </div>
  <div class="btn-group" role="group">
    <a href="#/forecast/5" class="btn btn-primary" ng-class="{  'btn btn-disabled' : days === '5' }">5 Day Forecast</a><a href="#/forecast/7" class="btn btn-primary" ng-class="{  'btn btn-disabled' : days === '7' }">7 Day Forecast</a><a href="#/forecast/10" class="btn btn-primary" ng-class="{  'btn btn-disabled' : days === '10' }">10 Day Forecast</a>
  </div>

However, when I run this, instead of replacing the Bootstrap CSS class for the element btn btn-primary and change it to btn btn-disabled, as desired; instead, it appends onto the element, so it becomes btn btn-primary btn-disabled all as one, and btn-primary overrules everything visually, so it winds up looking like this in the output —

<a href="#/forecast/1" class="btn btn-primary btn-disabled" ng-class="{  'btn btn-disabled' : days === '1' }">1 Day Forecast</a>

Naturally, this is not the desired effect I want here. Would someone please tell me how, in AngularJS ngClass, do i have it replace the Bootstrap CSS class, not just append it?

1
  • Please provide a plunkr or fiddle. Commented Nov 22, 2015 at 8:49

1 Answer 1

2

You are correct, using ng-class will add the classes when evaluated as true. So the solution is to add both options to ng-class so that either btn-primary or btn-disabled is added at any time.

<a href="#/forecast/1" class="btn" 
    ng-class="{'btn-disabled': days === '1', 'btn-primary': days !== '1'}">1 Day Forecast</a>

You also don't need to add the class btn to the ng-class since you want it to be used regardless of the value of days.

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

5 Comments

thank you for the answer. @Tristan. however, if i try the code your way, above, it still includes both CSS classes, btn-diabled and btn-primary, just now in reverse order. here is the HTML output from my dev tools in the browser — <a href="#/forecast/3" class="btn btn-disabled btn-primary" ng-class="{'btn-disabled': days === '3', 'btn-primary': days !== 3}">3 Day Forecast</a> ... see? it still does that, even coding it the way you suggest above. and because btn-primary is still in there, it takes precedence over everything. any other suggestions?
Try comparison to string. i.e. days !== '3'
Isn't days an int variable? @faddah, can you show us how $scope.days looks like? If you don't know the type of $scope.days, try == instead of ===
Considering @faddah's existing code was evaluating true (it was adding btn-disabled), days must be a string. The data type will just depend on how it's set: $scope.days = '1'; str, $scope.days = 1; int
that was it! @Tristan, you were so correct, i forgot to enclose the var in the second conditional in a '' for a string. i did that with all of them, it's now all working now, perfectly! thank you, @Tristan, thank you @Marin Takanov! you folks & stackoverflow are my new best friends!! 😉

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.