使用指令Angular JS动态更改文本的颜色

时间:2015-10-02 19:19:48

标签: javascript css angularjs

我的指令似乎不起作用,这是我的代码:

//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>

我希望能够动态更改范围的颜色:

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

通过附加一个类使用上面的指令加载表,但它没有任何效果。我的CSS是:

/*purple text */

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

我怎样才能让它发挥作用,谢谢!

1 个答案:

答案 0 :(得分:1)

如果我理解正确,并且您在user.profileColour中存储了所需的动态颜色选择,那么您可以执行以下操作

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

等等。

您可以将此抽象为传入user.profileColour的函数并返回类,具体取决于您希望逻辑的位置(如果您将其转换为函数,则可以在控制器中完成所有操作) 。所以像 -

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

并在控制器中

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

这假设所有profileColour都是字符串。