0

I got the buttons list from resource. It consist of 10 buttons like t1,t2,t3,t4,t5,t6,t7,t8,t9,t10

When I click the on t1 button first time button color change to red. Second time I click on same button color change to green.

Please tell me how to do this

<div class="col-sm-2 col-lg-1 col-md-1" ng-repeat="table in tables" style="margin-left:1px">
<button type="button" class="btn btn-success" ng-click="getTable(table)">{{table.tablename}}</button>
</div>

this is my method

 $scope.ng-click=gettable(table){
  //
  }

4 Answers 4

1

It's easy. Just bind one more property with your all the items in tables array like this-

for(var i = 0; i < $scope.tables.length; i++)
{
    $scope.tables[i].btnClass = "btn-success";
}

then in HTML, assign this class like this:

<button type="button" class="btn {{table.btnClass}}" ng-click="getTable(table)">
{{table.tablename}}</button>

and ng-click function as:

$scope.getTable = function(table) {
    table.btnClass = table.btnClass == "btn-info" ? "btn-success" : "btn-info"
}

Or if you want to change manual colors then create one class in css like this:

.red-button {
    background-color: "red";
}

Then in the function

$scope.getTable = function(table) {
    table.btnClass = table.btnClass == "red-button" ? "btn-success" : "red-button"
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your quick response.first time click on button btn-info is activated. but second time i click on the same button btn-success is not applied.
here how to set Default color is green for button. for loop not working for me
1

Here's my suggestion: Inside your $scope.ng-click=gettable(table){ ...... }

  • Create an array of colors
  • Everytime the button clicks, pull a random color value inside that array

Comments

0

Using the controllerAs syntax (which I advise you to use), you can work with the following.

html:

<div ng-repeat="table in ctrl.tables">
    <button type="button" ng-click="ctrl.getTable(table)" ng-style="{ 'background-color': table.color }">{{table.tablename}}</button>
</div>

js:

this.getTable = function(table) {
    table.color = "red";
};

https://plnkr.co/edit/axzyUQJowlSEClszrIQs

2 Comments

how to set default color green for button
Well just use the style attribute on the button... <button type="button" ng-click="ctrl.getTable(table)" ng-style="{ 'background-color': table.color }" style="background-color: green;">{{table.tablename}}</button>
0
var colors=['red','green','yellow','black','blue'];
$scope.color=null;
$scope.ng-click=gettable(table){ 
var colorToApply=colors[Math.floor(Math.random()*colors.length)];
$scope.color=colorToApply
 }

Now you can define css rule here

.green{
  color:green;
}
.blue{
  color:blue;
}
.red{
  color:red;
}
.yellow{
  color:yellow;
}

Then in your view you can set ng-style on the button you want to change color and bind it to color in the scope like

<button ng-style="color">Change color</button>

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.