最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Toggle class between two buttons in Angular but restrict to prevent same button from changing class - Stack Overflo

programmeradmin1浏览0评论

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

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

Share Improve this question asked Apr 27, 2015 at 18:05 isaac weathersisaac weathers 1,4825 gold badges29 silver badges54 bronze badges 1
  • simple assing static value like true or false instead yupNope.isSelected = !yupNope.isSelected – Grundy Commented Apr 27, 2015 at 18:10
Add a ment  | 

3 Answers 3

Reset to default 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

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.

You can acplish 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/api/ng/directive/ngDisabled

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论