0

I would like to be able to add a CSS class to the text color of a message based on the response.

Currently I do it this method:

if (response.data.status == 201) {
    angular.element(document.getElementById("msg")).css("color", "green");
    $scope.msg = 'Users created for Meeting ' + r.id
        + ' on ' + $filter('date')(r.updated_at, 'M/d/yyyy')
        + ' at ' + $filter('date')(r.updated_at, 'HH:mm:ss');
    console.log("Status Code= " + response.data.status + ", Status Text= " + response.data.message);
    return true;
}
else {
    angular.element(document.getElementById("msg")).css("color", "red");
    $scope.msg = r.message;
    angular.element("#meeting-id").focus()
    console.log("Status Code= " + response.data.status + ", Status Text= " + response.data.message);
    
    return false;
}                        

and it works, but now I have two classes that can be added to the success and error messages. I need to figure out how to add them so that I can remove the style attribute that changes the text to red or green.

The classes names are:

•   Success: “text-green”

•   Error: “text-red”

What is the method to correctly achieve this?

Any help would be great.

Thank you, Erasmo

erasmo carlos
  • 664
  • 5
  • 16
  • 37

1 Answers1

1

You can use ng-class for this
in js file

      $scope.responseCode = response.data.status;

then in html

    <div ng-class="{'text-success':responseCode == 201,'text-danger':responseCode != 201}">
        <h1>Welcome Home!</h1>
        <p>I like it!</p>
    </div>

Refer this more about ng-class: https://docs.angularjs.org/api/ng/directive/ngClass

or Using classList.add:

For success message you can use

     angular.element(document.getElementById("msg")).classList.add('green');

For Error message you can use

angular.element(document.getElementById("msg")).classList.add('red');
Vijay Palaskar
  • 526
  • 5
  • 15