Unable to show the table row color using Angular in Internet Explorer

I've customized a table by changing the row color based on a property value, and I'm using ng-repeat to display the rows. Here's my table:

<table class="tab table span6 tabhover" style="margin:0;" >
            <thead>
                <th>No. Parte</th>
                <th>Cantidad</th>
                <th>Descripcion</th>
                <th>Estatus</th>
                <th>Fecha estatus</th>
            </thead>
            <tbody ng-repeat="r in reqs">
               <tr ng-class="{selected: r.idrdm_detalles == idSelectedVote}"   ng-click='detallesHistory(r.idrdm_detalles)'    bgcolor="{{r.color}}"> 
                <td>{{r.numeroparte}}</td>
                <td>{{r.cantidad}}</td>
                <td>{{r.descripcion}}</td>
                <td>{{r.estatus}}</td>
                <td>{{r.fecha_estatus}}</td>
               </tr> 
            </tbody>
        </table>

The background color of each row is determined by the "color" property of the object.

For setting the row color, I've implemented this function:

this.color = function(codigo){
            if(codigo == 10){
                return '#00A6E8';
            }
            else if(codigo == 60 || codigo == 80){
                return '#51BD53';
            }
            else if(codigo == 90 || codigo == 99){
                return '#DE5F5F';
            }
            else{
                return '#F5AB2C';
            };
        };

In my controller, I use Angular forEach to add the color property to each data array entry:

angular.forEach(data, function(value, key){
                    var c = value.codigo;
                    value.color = self.color(c);

                    $scope.idrdm = req; 
                    //console.log(value);
                });
                $scope.reqs = data;

The row background colors are displayed correctly in browsers like Chrome, Firefox, and Safari. However, in Internet Explorer, the color appears as "#00c00" instead of the expected color. If anyone has insight on how to address this issue, please let me know. PS. I tried using ng-style like this:

<tr ng-style="'{background-color':{{r.color}}"> 

This method worked for other browsers but not in Internet Explorer.

Answer №1

Personally, I believe that color values should be kept in CSS rather than Javascript. A potential solution to this issue could be organizing your code in the following way. It's important to use more descriptive class names instead of generic ones for better clarity.

JS

this.color = function(codigo){
            if(codigo == 10){
                return 'blue';
            }
            else if(codigo == 60 || codigo == 80){
                return 'green';
            }
            else if(codigo == 90 || codigo == 99){
                return 'red';
            }
            else{
                return 'darkblue';
            };
        };

HTML

<tr ng-class="'{{r.color}}'"> 

CSS

.blue {
    background: #00A6E8;
}
.green {
    background: #51BD53;
}
.red {
    background: #DE5F5F;
}
.darkblue {
    background: #F5AB2C;
}

Answer №2

To implement the ng-style, use this format:

<tr ng-style="{'background-color': r.color}">

For a demonstration, check out this plunker: http://plnkr.co/edit/KmIO5MVXlsBSitTXs19k?p=preview

If you prefer to utilize bgcolor, you can achieve it by using ng-attr-* in the following way:

<tr ng-attr-bgcolor="r.color">

I hope this explanation is beneficial.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What could be causing my ng-grid footer to refuse to align with the bottom border?

Currently utilizing ng-grid and various AngularJS UI Bootstrap components on my website, I have encountered a recurring issue. By diligently investigating, I have successfully replicated the problem. Access the Plunker demonstration through this link. The ...

Sending a javascript variable to an angularjs scope

Currently, I am utilizing Flask to render an HTML template and wish to transfer the variable add_html_data, which is passed via Flask's render_template, to the scope of an AngularJS controller. I have attempted the following: <body> <di ...

Why is the "class" attribute of an SVG node not being applied when I change it?

I am having trouble changing the "class" attribute of a node in SVG using my AngularJS Directive. Even though I've written the code to do so, it doesn't seem to be applied properly. This is the code snippet from my Directive: node = node.data(f ...

Modify the parent's style in CSS based on the presence of certain specified children

I'm working with an HTML element that can contain a child with the ID 'slideshow' <div id='content'> <div id='slideshow'> </div> </div> Alternatively, it could have a different child like t ...

Using AngularJS ui-router ui-sref results in the error message "Uncaught TypeError: Cannot read property '0' of undefined."

I am currently working on an angularJS component that utilizes ui-router with 2 straightforward route states. export default function Routes($stateProvider, $urlRouterProvider, $locationProvider) { $stateProvider .state('details', { ...

What is the latest CSS browser support for the min() and max() functions in 2018?

I am considering implementing the use of the min() and max() functions in my project. I'm curious about the current browser support as of 2018. Do you think it is necessary to include the @supports at-rule? For example, I could write: .px-4-safe { ...

Choosing the initial offspring of an element that do not share a common parent

My question might not be perfectly phrased, for which I apologize. Is there a CSS method to select only the first child of an element without affecting others that are not grouped under the same parent? For instance: <div class="parent"> <div ...

AngularJS Blob method causing downloaded documents to become corrupted

I was able to download files successfully in my application until I upgraded Angular to the latest version. However, after the upgrade, the downloaded files are getting corrupted. The upload functionality is still working fine and the files on the server a ...

Using AngularJS with angular-google-maps to easily add markers through a form and locate your position with one click

I'm attempting to replicate the functionality demonstrated on since it fulfills 90% of my requirements. However, I'm facing some difficulties. While I can retrieve my location and log it in the console, a marker is not appearing on the map. Add ...

Adjusting the color of specific sections within a text box

Can I change the color of a specific section of a text input box? I'm working on a comment widget that needs everything between the @ and : symbols to be in a different color: <input type="text" placeholder="Want To Say Something?" value="@user55 ...

Deleting various classes (jQuery)

Is there a more effective alternative to rewriting the following code snippet? $('element').removeClass('class1').removeClass('class2'); I am unable to utilize removeClass(); because it would eliminate ALL classes, and that ...

How can I ensure that the HTML I retrieve with $http in Angular is displayed as actual HTML and not just plain text?

I've been struggling with this issue for quite some time. Essentially, I am using a $http.post method in Angular to send an email address and message to post.php. The post.php script then outputs text based on the result of the mail() function. Howev ...

Is it possible to navigate to a different section of a webpage while also jumping to a specific id within that section seamlessly?

I'm trying to create a navbar link that will take me directly to a specific section of a page, but I'm having trouble getting it to work. Here's what I've tried: <a href="home#id" class="nav-link text on-hover"></a> Where ...

When does the ng-disable function become activated?

Here's an example: <button ng-disabled="!isSomethingValid() || loading || disabled" ... class="btn btn-primary"> What determines the condition for the ng-disable attribute to evaluate its expression? ...

Tips for highlighting HTML syntax within JavaScript strings in Sublime Text

Is there a Sublime package available for syntax highlighting HTML within JavaScript strings specifically? (Please note that the inquiry pertains to highlighting HTML within JS strings only, not general syntax highlighting.) Currently, I am developing Ang ...

Instead of returning a JSON result, the MVC controller method called from AngularJS is returning the view HTML

Within the HomeController, I have the following method, [HttpGet] public ActionResult GetStudents() { Student std1 = new Student(); List<Student> stdlst = new List<Student>(); std1.Id = 1; ...

Exploring the possibilities of ReactJS and the sleek design of

How can I alter the background color of a RaisedButton without having the CSS class name appear in the autogenerated div? Here is the button: <RaisedButton className="access-login" label="Acceder" type="submit"/> This is the specified CSS: .acces ...

Properly Positioning Text within React's Material UI Components

I am encountering a simple issue where I am trying to align '01/01/2020' with 'A00002'. To view my code on CodeSandbox, please visit CLICK HERE <div className={classes.root}> <Typography variant="h6" className={cla ...

Transform the numerical character into a checkbox within a table using AngularJS

In my HTML code, I am utilizing AngularJS to present data in a table. <div ng-controller="CheckCtrl"> <table class="table table-hover data-table sort display"> <thead> <tr> <th class="Serial_"> ...

How can you set a checkbox to be selected when a page loads using Angular?

On page load, I need a checkbox to already be 'checked', with the option for the user to uncheck it if they want. Despite trying to add [checked]="true" as recommended in some Stack Overflow answers, this solution is not working for me. <label ...