2

I am trying to change my button color dynamically but not achieved can someone suggest help

   <a  class="button buttonaquacss button-mini button-aqua  text-right pull-right"  (click)='send(button,detail.profile_id)' #button  [ngStyle]="{'background-color':  color}"><span><i class="icon-plus-sign"></i>{{detail.innerHTML}}</span></a>   




  if (detail.connection_status == '') {
                    detail.innerHTML = "connect";
                    this.color = "red";
                }




send(button, index): void {

    var str = localStorage.getItem('social');
    var loc = JSON.parse(str);
    var id = loc.profile_id;
    var formdata = { recieved_id: index, sent_id: id }

    this.color = 'blue';
    var headers = new Headers();

    headers.append('Content-Type', 'application/x-www-form-urlencoded')
    this.http.post('http://localhost/a2server/index.php/requests/sendrequest/', formdata, { headers: headers })
        .subscribe(
        response => {
            if (response.json().error_code == 0) {
                button.innerHTML = "Pending";
                button.disabled = true;
                this.color = "#127bdc";

            }
            else {
                button.innerHTML = "Unable to send request";
            }
        });


}

I am trying to change my button color dynamically but not achieved can someone suggest help

3 Answers 3

4

You can leverage the ngStyle directive:

<a  class="button buttonaquacss button-mini button-aqua  text-right pull-right" 
    [ngStyle]="{'background-color':  color}">
  (...)
</a>
Sign up to request clarification or add additional context in comments.

6 Comments

It should ;-) Could you add the content of your send method into your question? Thanks!
It does not change because you already set the static code here. this.color = "#127bdc"; You should get it from somewhere and not set there.
Is the following block executed: if (response.json().error_code == 0) { ... }?
So that's the problem. Your button is always blue... Where does the block take place in your component: if (detail.connection_status == '') { ... }?
That comes with costructor and the condition is always true
|
0

A simple solution would be using ngStyle ( https://docs.angularjs.org/api/ng/directive/ngStyle ) using directive like this

ng-style="{color: myColor}"

directing to your controller.

Comments

0

Another method to solve this problem would be to have a conditional class that overrides the color using [class.newColor]="errorThrown" This holds true to the principle of separating your logic from your styling.

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.