2

Trying to create some very basic functionality which basically makes two buttons behave like radio buttons. Have two buttons that are used to show/hide different content based on what is shown. So it is working as expected except for it is changing on any click. So if I click the same button twice then it changes the class. I only need it to change if the other button is clicked:

<button class="someClass" ng-class="{true: 'green', false: 'blue'}        [!yupNope.isSelected]" ng-click="yupNope.isSelected = !yupNope.isSelected">YES</button>
   <button class="someClass" ng-class="{false: 'green', true: 'yupNope'}[!yupNope.isSelected]" ng-click="yupNope.isSelected = !yupNope.isSelected">NO</button>

So if Yes is clicked then it should update and No should too. But if Yes is clicked again before No is clicked then there should be no change.

Here is a Plunkr to play with:

Plunkr Demo

1
  • simple assing static value like true or false instead yupNope.isSelected = !yupNope.isSelected Commented Apr 27, 2015 at 18:10

3 Answers 3

3

Instead of yupNope.isSelected = !yupNope.isSelected as ngClick expression set yupNope.isSelected to true/false respectively:

<button ng-click="yupNope.isSelected = false" class="someClass" ng-class="{true: 'green', false: 'blue'}[!yupNope.isSelected]">YES</button>
<button ng-click="yupNope.isSelected = true" class="someClass" ng-class="{false: 'green', true: 'yupNope'}[!yupNope.isSelected]">NO</button>

Demo: http://plnkr.co/edit/HJfmYf6d9dnNqZLwYLFF?p=preview

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

3 Comments

i'm so slow write answer :-)
@Grundy Sorry, next time you will beat me ;)
I'll be faster next time :-D
2

Add a condition to ng-click expression:

<button class="someClass" ng-class="{true: 'green', false: 'blue'}[!yupNope.isSelected]" 
  ng-click="yupNope.isSelected ? (yupNope.isSelected = !yupNope.isSelected) : ''">YES</button>
 <button class="someClass" ng-class="{false: 'green', true: 'yupNope'}[!yupNope.isSelected]" 
  ng-click="!yupNope.isSelected ? (yupNope.isSelected = !yupNope.isSelected) : ''">NO</button>

Your modified plunkr.

1 Comment

Admittedly dfsq's answer's better :)
0

You can accomplish this by using ng-disabled. On each button you can apply the ng-disabled attribute.

<button class="someClass"
    ng-class="{true: 'green', false: 'blue'}[!yupNope.isSelected]" 
    ng-click="yupNope.isSelected = !yupNope.isSelected"
    ng-disabled="yupNope">YES</button>

<button class="someClass"
    ng-class="{false: 'green', true: 'yupNope'}[!yupNope.isSelected]"
    ng-click="yupNope.isSelected = !yupNope.isSelected"
    ng-disabled="!yupNope">NO</button>

You can find the docs here: https://docs.angularjs.org/api/ng/directive/ngDisabled

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.