bobo2000
bobo2000

Reputation: 1877

Dynamically changing colour of the text using directive Angular JS

My directive does not seem to be working, here is my code:

//profile colour directive
app.directive('profileColour', function() {
    return {
        restrict: 'A',
        link: function(scope, element, attrs) {
            var imageDiv = scope.$eval(attrs['profileColour']).imageId;
            var colour = scope.$eval(attrs['profileColour']).colour;
            var divName = '#name' + imageDiv;
            //$(divName).addClass("purpleText");
            $(divName).addClass("purpleText");
        }
    };
});

HTML:

            <table class="table table-striped table-hover">
                <thead>
                    <tr>
                        <th class="col-xs-2">
                            <span></span>
                        </th>
                        <th class="col-xs-8" ng-click="sort('firstName')">
                            <span class="glyphicon sort-icon" ng-show="sortKey=='firstName'" ng-class="{'glyphicon-chevron-up':reverse,'glyphicon-chevron-down':!reverse}"></span>
                        </th>
                        <th class="col-xs-2">
                            <span></span>
                        </th>
                    </tr>
                </thead>
                <tbody>
                    <tr ng-click="showModal($event, user.emailAddress)" change-image="{imageId: {{$index}}, colour: 'blue'}" dir-paginate="user in users|orderBy:sortKey:reverse|filter:search|itemsPerPage:5">
                        <td>
                            <!--img class="round" src="/images/profile-placeholder.jpg" width="50" height="50">
                                </img> -->
                            <img class="round" src={{user.profileImageUrl}} width="50" height="50"></img>
                        </td>
                        <!-- <td><img src={ {user.profileImageUrl}} width="100" height="100"></img></td> -->
                        <td>
                            <div style="padding-top:1em"><span profile-colour="{imageId: {{$index}}, colour: 'blue'}" id='name{{$index}}'>{{user.firstName}}</span>
                                <br>{{user.lastName}}
                                <br>{{user.profession}}</div>
                        </td>
                        <td>
                            <div style="padding-top:1em">
                                <img id={{$index}} src="images/arrow-right-purple.png" width="50" height="50"></div>
                        </td>
                    </tr>
                </tbody>
            </table>

I want to be able to dynamically change the colour of the span:

> <span profile-colour="{imageId: {{$index}}, colour: 'blue'}"
> id='name{{$index}}'>{{user.firstName}}</span>

upon the table loading using the above directive by attaching a class, but it is not having any effect. My CSS is:

/*purple text */

.purpleText {
    color: #6c58bF;
    font-weight: 900;
    text-transform: uppercase;
    font-style: bolder;
}

How can I get this to work, thanks!

Upvotes: 0

Views: 1064

Answers (1)

ajmajmajma
ajmajmajma

Reputation: 14216

If I am understanding this correctly, and you have the desired dynamic color choice stored in user.profileColour, then you can do something like this

<span  ng-class="{ 'purpleText' : user.profileColour === 'purple';  'greenText' : user.profileColour === 'green'}"> 

And so on.

You can abstract this to a function where you pass in user.profileColour and return the class as well, depending on where you want the logic (if you turn it into a function you could have it all in the controller). So something like -

<span ng-class="setColor(user.profileColour)" >

and in the controller

$scope.setColor = function(color) {
  //assuming profileColour is purple this would return "purpleText"
  return color + "Text";
}

This is assuming all the profileColour are strings.

Upvotes: 1

Related Questions