0

Here is code to click button text color using AngularJS.

<button ng-class="myClass"
   ng-click="myClass='red'"
   ng-init="myClass='blue'">
some text
</button>

http://jsfiddle.net/rLkb5/2/

Is it possible to change the color to green on the second click? (blue->red->green)

My gut tells me it's impossible without controller+custom JS code but there may be a simple way.

2
  • 1
    Why do you feel the need to do a state change operation without a controller? Doesn't this state need to be stored somewhere anyway? The solutions provided below will work, but they require you to put a fair bit of logic into your template, which is inelegant, will hinder testing, and will make the code less portable. Can you elaborate on your use case a little more? Commented Mar 29, 2013 at 4:23
  • @JoshDavidMiller There is no use case. I just asked if it is possible to do foo in bar way. It sounded strange for me that some method works with 2 states but not with 3 states, though I understand the first red->blue is not about states. Commented Mar 29, 2013 at 11:55

3 Answers 3

1
function MyCtrl($scope) {
  $scope.active = false;
  $scope.toggleActive = function () {
    $scope.active = !$scope.active; 
 };
}

.

<div ng-class="{red: !active, green: active}" ng-click="toggleActive()"></div>

ngClass, when given an object, sets the classes of which property values are truthful.

Note: View's should not contain any business logic as they are hard to test; they only should interact with the controller.

Clarifition on that:

What you shouldn't do is, to change properties in scope directly from your view. Have a function in controller acts as a wrapper which does that for you. View can change how itself is rendered depending on the scope properties and call functions in controller to manipulate data. Also, controllers should not aware of at all how you want to display the data, it should behave like there is no view at all (That in fact, view should be replacable). Which css class you want to apply is not something your controller should know / decide.

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

2 Comments

Right, View's should not contain business logic. Then shouldn't I even use the first example to change color from red to blue using View?
Got it! Too bad I don't have right to vote up yet. I hope somebody else does.
0

Check if this is acceptable

<div ng-app>
    <button ng-class="myClass" ng-click="counter = counter + 1; myClass=classes[counter % 3]" ng-init="myClass='blue'; classes=['blue', 'red', 'green']; counter=0">some text</button>
</div>

Demo: Fiddle

1 Comment

Hmm, a bit more robust than my example :D
0

There is probable a better way, but if you rename your classes:

.statetrue{
  color:red
}

.statefalse{
  color:blue
}

Then, your button:

<button ng-class="'state' + myClass" ng-click="myClass = !myClass"  ng-init="myClass=true">

1 Comment

Where is green by the way? :)

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.